扫码一下
查看教程更方便
Javascript prototype 属性允许您向任何对象(数字、布尔值、字符串和日期等)添加属性和方法。
当构造一个原型,所有的日期对象都会默认添加属性和方法。
注意- 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