Javascript Object.prototype
作者:--
发布时间:2019-11-20
评论:0
阅读:0
prototype属性可以将属性和方法添加到任何对象(number, boolean, string 和date等)。
注:原型(prototype)是一个全局的属性,它可以使用在几乎所有的对象。
语法
object.prototype.name = value
实例:
这里有一个例子展示了如何使用原型(prototype)属性的属性添加到对象:
<html>
<head>
<title>user-defined objects</title>
<script type="text/javascript">
function book(title, author){
this.title = title;
this.author = author;
}
</script>
</head>
<body>
<script type="text/javascript">
var mybook = new book("perl", "mohtashim");
book.prototype.price = null;
mybook.price = 100;
document.write("book title is : " + mybook.title + "<br>");
document.write("book author is : " + mybook.author + "<br>");
document.write("book price is : " + mybook.price + "<br>");
</script>
</body>
</html>
这将产生以下结果:
book title is : perl
book author is : mohtashim
book price is : 100