1、描述
prototype
属性允许为任何对象(Number
, Boolean
, String
和 Date
等)添加属性和方法。
注意:prototype是一个全局属性,几乎所有的对象都可用。
2、语法
它的语法如下:
object.prototype.name = value
3、例如
<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("C#", "cjavapy"); 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>