prototype 属性使您有能力向对象添加属性和方法。
语法
object.prototype.name=value
<html>
<head>
<script language="javascript" type="text/javascript">
//对象的定义,使用(名片示例)
function Card(name, address, work, home){
this.name = name;
this.address = address;
this.work = work;
this.home = home;
this.show = ShowCard;
}
function ShowCard(){
l1 = "name:" + this.name + ".<br/>";
l2 = "address" + this.address + ".<br/>";
l3 = "work" + this.work + ".<br/>";
l4 = "home" + this.home + ".<br/>";
document.write(l1,l2,l3,l4);
}
var tom = new Card("tom","河口","程序员","北京");
tom.show();
//向String对象添加一个heading方法。将字符串显示为标题,然后用这个方法显示3个标题
function addhead(level){
html = "H" + level;
text = this.toString();
a = "<" + html + ">";
b = "<" + html + "/>";
return a + text + b;
}
String.prototype.heading = addhead;
document.write("this is h1".heading(1)+"</br>");
document.write("this is a h3".heading(3)+"</br>");
document.write("this is a h5".heading(5)+"</br>")
</script>
</head>
</html>