function myConstractor(message) { this.myMessage=message; //私有属性 var separator='-'; var myOwner=this; //私有函数 function alertMessage() { alert(myOwner.myMessage); } alertMessage(); //特权方法(也是公有方法) this.appendToMessage=function(string){ this.myMessage+=separator+string; alertMessage(); } //静态属性、方法 myConstractor.name='Jeff'; myConstractor.alertName=function() { alert(this.name); } //公有方法 myConstractor.prototype.clearMessage(string) { this.myMessage=''; } }
各种成员的调用:
var myObject=new myConstractor(‘Hello Jo’);
myObject.alertMessage(); //错误,不能调用私有成员函数
myObject.separator;//错误不能调用私有成员属性
myObject.appendToMessage(‘Yeah!’);
var name=myConstracor.name;
myConstractor.alertName();
myObject.clearMessage();
myConstractor.clearMessage();//错误,myObject才是myConstracor的实例,myConstracor是Function的实例。