定义和用法
prototype 属性使您有能力向对象添加属性和方法。
语法
object.prototype.name = value
添加属性实例
在本例中,我们将展示如何使用 prototype 属性来向对象添加属性:
<script type="text/javascript">
function employee(name,job,born) {
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
employee.prototype.salary=null;
bill.salary=20000;
document.write(bill.salary);
</script>
输出:
20000
添加方法实例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<script type="text/javascript">
function employee(name,job,born) {
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
// 向对象添加属性
employee.prototype.salary = null;
bill.salary=20000;
// 向对象添加方法
employee.prototype.funcc = function (op) {
document.write(123456);
}
document.write(bill.salary);
bill.funcc();
</script>
</body>
</html>
输出:
20000 123456
参考源于:
http://www.runoob.com/try/try.php?filename=tryjsref_prototype