prototype 属性可让您向任何对象 (Number,Boolean,String和Date等)添加属性和方法。
使用以下语法创建布尔原型。
object.prototype.name=value
prototype - 示例
下面的示例演示如何使用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", "Tom");
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 : Tom Book price is : 100