javascript 重要属性之prototype(继承)

转载猫猫小屋 http://www.maomao365.com/?p=831

在javascript中每一个函数都拥有 prototype属性,
在javascript中使用prototype,可以向已经生成的对象中添加方法和属性。
例: 为一个对象 动态添加属性
< script type="text/javascript">
function blog(blogName,blogQty)
{
this.name=blogName;
this.qty=blogQty;
}

var b=new blog(“maomao365.com”,”1″);
blog.prototype.all=null;
b.all=888;

blog.prototype.alert=function(){
alert(“动态添加函数!”);
}
b.alert();
document.write(b.all);
< /script>
————–类似于————-
< script type="text/javascript">
function blog(blogName,blogQty)
{
this.name=blogName;
this.qty=blogQty;
}

blog.prototype={
all:’888′,
alert:function(){
alert(“动态添加函数!”);
}
};

var b=new blog(“maomao365.com”,”1″);
b.alert();
document.write(b.all);

< /script>

原文地址:https://www.cnblogs.com/lairui1232000/p/4306264.html