目录

prototype

prototype属性允许您向任何对象添加属性和方法(Number,Boolean,String和Date等)。

Note - Prototype是一个全局属性,几乎可用于所有对象。

使用以下语法创建布尔原型。

object.prototype.name = value

例子 (Example)

以下示例显示如何使用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
↑回到顶部↑
WIKI教程 @2018