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

javascript的prototype经典使用场景

2014-07-01 19:31 295 查看
prototype的经典使用场景就是为对象增加属性和方法,如给自定义的Man对象增加个姓名属性和语言方法:

function man() {
this.age = "22";
}
var tom = new man();
man.prototype.name = "tom";
man.prototype.say = function () {
alert("english");
};
alert(tom.name);
tom.say();


结果:弹出”tom”,弹出”english”

prototype同样可以作用于ECMAScript对象,如对字符串对象增加一个清除方法(这个方法可能没有使用场景..):

String.prototype.clean=function(){
return "";
};
var str="三尺龙泉万卷书";
alert(str.clean());


结果:弹出空白对话框
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: