您的位置:首页 > Web前端 > JavaScript

js中的公有、私有、特权和静态成员

2012-02-02 10:23 288 查看
合理使用JS中的各种成员类型有利于我们的代码更好的被使用和理解

下面我举一个简单的例子详细说明下:

function myConstructor(message) {
this.myMsaage = message;
//私有属性
var separate = ' -';
var myOwner = this;

//私有方法
function alertMessage(){
alert(myOwner.myMsaage);
}
alertMessage();

//特权方法(也是公有方法)
this.appendToMessage = function (string){
this.myMsaage += separate + string;
    alertMessage();
}
  //公有方法
myConstructor.prototype.clearMessage = function(){
this.myMsaage = '';
}
//静态属性
myConstructor.name = 'Jeff';
//静态方法
myConstructor.alertName = function(){
alert(this.name);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: