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();