/* -- 封装 -- */
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);//undefinedconsole.log(_packaging._age);//15console.log(_packaging.sex);//undefinedvar abc = new _packaging();
console.log(abc.age);//undefined
console.log(abc.title);//JavaScript Design Patternsconsole.log(abc._age);//undefinedconsole.log(abc.sex);//male abc.getAge();//12abc.init();//age is not defined