javascript的prototype经典使用场景
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());结果:弹出空白对话框
posted on 2017-05-27 12:53 cynchanpin 阅读(557) 评论(0) 收藏 举报
浙公网安备 33010602011771号