正文
Static variables in JavaScript
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
function MyClass () { // constructor function
var privateVariable = "foo"; //NO:obj.privateVariable, MyClass.privateVariable; PS:不属于instance,只能在内部使用;
this.publicVariable = "bar"; //YES:obj.publicVariable; No:MyClass.publicVariable;
this.privilegedMethod = function () { //YES:obj.privilegedMethod(); NO:MyClass.privilegedMethod();
alert(privateVariable);
};
} MyClass.prototype.publicMethod = function () { //YES: obj.publicMethod();
alert(this.publicVariable);
}; MyClass.staticProperty = "baz"; //YES: MyClass.staticProperty; //...
var myInstance = new MyClass();