A private variable: a property and method defined inside a function is called the function's private property and method common variables: Objects and methods created inside a function called this function are common properties and methods
Privileged methods: a common property and method created through this can access both the function and the private properties and methods of the function called the privileged method
Constructor: The object invokes the privileged method at creation time to initialize the properties of the instance, also known as the constructor
/**
* 类的内部私有属性、私有方法、特权方法、共有属性和共有方法以及构造器
* @param id
* @param bookname
* @param price
* @constructor
*/
var BookDemo = function(id,bookname,price){
/*
*申明在函数内部的属性和方法是该函数对象的私有属性和私有方法
*/
var num = 1;// 私有属性
//私有方法
function checkId(id){
return id;
}
/**
* 通过this创建的属性和方法是共有属性和方法 也称为特权方法
*/
this.bookname = bookname;
this.getBookName = function(){
console.info(‘getName特权方法,可以调用类的私有属性和方法:num=‘+num);
}
this.setBookName = function(bookname){
console.info(‘setBookName特权方法‘);
}
this.copy = function(){
console.info(‘copy的共有方法‘)
}
/**
* 在对象创建时候调用这些共有方法初始化实例对象的属性,那么这些共有方法又称为构造器,
* 在对象被new出来的时候就会被执行 构造器必须有对应的特权方法的实现,否则就会报错
*
This.setbookname is not a function
*/
this.setBookName(bookname); //如果包含方法则该方法会在初始化的时候就会被创建
this.getBookName();
}
To invoke the test method:
var bookDemo = new BookDemo(‘1‘,‘Javascript设计模式‘,‘48.5‘);//由于调用了 this.setBookName 和this.getBookName 属性通过new创建实例的时候就会执行 this.getBookName和 this.setBookName 的特权方法
- The result of all outputs is: the Setbookname privileged method and the GetName privileged method, which can invoke the private properties and methods of the class: Num=1
Continue calling test method
console.info(bookDemo.num); //
Undefined
Modify Bookdemo to track changes in BookName
/**
* 类的内部私有属性、私有方法、特权方法、共有属性和共有方法以及构造器
* @param id
* @param bookname
* @param price
* @constructor
*/
var BookDemo = function(id,bookname,price){
/*
*申明在函数内部的属性和方法是该函数对象的私有属性和私有方法
*/
var num = 1;// 私有属性
//私有方法
function checkId(id){
return id;
}
/**
* 通过this创建的属性和方法是共有属性和方法
*/
this.bookname = bookname;
this.getBookName = function(){
console.info(‘getName特权方法,可以调用类的私有属性和方法:num=‘+num+",bookname="+bookname);
}
this.setBookName = function(bookname){
console.info(‘setBookName特权方法,bookname=‘+bookname);
}
this.copy = function(){
console.info(‘copy的共有方法‘)
}
/**
* 在对象创建时候调用这些共有方法初始化实例对象的属性,那么这些共有方法又称为构造器,
* 在对象被new出来的时候就会被执行
*/
this.setBookName(bookname);
this.getBookName();
}
Test
var bookDemo = new BookDemo(‘1‘,‘Javascript设计模式‘,‘48.5‘);
bookDemo.setBookName(‘Java设计模式‘);
/**
* 输出依次为:setBookName特权方法,bookname=Javascript设计模式
* getName特权方法,可以调用类的私有属性和方法:num=1,bookname=Javascript设计模式
* setBookName特权方法,bookname=Java设计模式
*/
Static common method and static common property of class
/***
* 通过new创建对象是,由于类外面通过点语法创建的属性和方法没有执行到,所以新创建的对象无法获取到他们
* 但是可以通过类来获取,所以称之为类静态共有属性和方法
* @type {boolean}
*/
BookDemo.isChinese = true; //类静态共有属性
//类静态共有方法
BookDemo.checkBookName = function(){
console.info(‘通过点方法创建的类静态共有方法,只能改类自己直接方法,不能通过new创建对象来访问‘);
}
/**
* 通过prototype创建的属性和方法可以通过this来访问,所以通过prototype创建的属性和方法称之为共有属性和方法
* @type {{isJsBook: boolean, display: BookDemo.display}}
*/
BookDemo.prototype = {
isJsBook : true,
display : function(){
console.info(‘外部通过prototype创建的方法可以访问该函数内部的属性,bookname=‘+this.bookname);
}
}
Test
< Span class= "PLN" >console info ( bookdemo ischinese //undefined
bookdemo checkbookname (); //the class static common method created by the point method, can only be changed to the class itself direct method, cannot create object through new to access
Bookdemo display (); //the properties inside the function can be accessed through a method created by prototype, bookname=undefined
From for notes (Wiz)
JavaScript's private variables and methods, common variables and methods, and privileged methods, constructors, static common attributes, and static common methods