JavaScript对象-原型 说明 原型属性允许您向任何对象(Number、Boolean、String和Date等)添加属性和方法。 注意: 原型是一个全局属性,可用于几乎所有对象。 语法 其语法如下− object.prototype.name=value HTMLCopy 例子 请尝试以下示例。 <html> <head> <title>用户定义对象</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("书的标题是:" + myBook.title + "<br>"); document.write("书的作者是:" + myBook.author + "<br>"); document.write("书的价格是:" + myBook.price + "<br>"); </script> </body> </html> HTMLCopy 输出 书的标题是:Perl 书的作者是:Mohtashim 书的价格是:100 HTMLCopy