1. 程式人生 > >程式碼-JS之靜態屬性和私有屬性

程式碼-JS之靜態屬性和私有屬性

/***************** 靜態屬性 *****************/ //靜態方法的特點就是可以直接使用建構函式來呼叫 function A() { this.age = 10; //公開的非靜態的方法 A.sex = '男'; //定義靜態屬性 A.PI = 3.141592653; //定義靜態屬性 A.max = function(){ //定義靜態方法 console.log(123); }; } var a = new A(); //必須呼叫一下A函式
console.log(A.sex); A.max(); console.log(A.PI); /***************** 私有屬性 *****************/ function B(){ this.age = 10; //公開屬性 var sex = '男'; //私有屬性 this.say = function(){ console.log('我是' + sex + '的'); } } var b = new B(); console.
log(b.age); //結果:10 console.log(b.sex); //結果:undefined b.say();