prototype属性允许您向任何对象(Number, Boolean, String和Date)添加属性和方法。本文主要介绍JavaScript(JS) object.prototype属性。

1、描述

prototype属性允许为任何对象(Number, Boolean, StringDate等)添加属性和方法。

注意: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>

推荐文档

相关文档

大家感兴趣的内容

随机列表