JS 封装的结构关系
/* -- 封装 -- */
var _packaging = function() {
//私有属性和方法
var age = "12";
var method1 = function() {
//...
}
//特权属性和方法(1.可以被实例访问 2.不能被类访问 3.可以访问私有属性)
this.title = 'JavaScript Design Patterns';
this.getAge = function() {
console.log(age);
return age;
}
}
//共有静态属性和方法(1.可以被类访问 2.不能被实例访问)
_packaging._age = '15';
_packaging.alertAge = function() {
alert(_packaging._age);
}
//共有属性和方法(1.可以被实例访问 2.不能被类访问 3.不可以访问私有属性)
_packaging.prototype = {
sex:"male",
init: function() {
console.log(age);
}
}
console.log(_packaging.age);//undefined
console.log(_packaging.title);//undefined
console.log(_packaging._age);//15
console.log(_packaging.sex);//undefined
var abc = new _packaging();
console.log(abc.age);//undefined
console.log(abc.title);//JavaScript Design Patterns
console.log(abc._age);//undefined
console.log(abc.sex);//male
abc.getAge();//12
abc.init();//age is not defined