Miêu tả

Thuộc tính prototype cho phép bạn thêm các thuộc tính và phương thức tới bất kỳ đối tượng nào (Number, Boolean, String và Date …).

Ghi chú − Prototype là một thuộc tính Global mà có sẵn với hầu hết tất cả đối tượng.

Cú pháp

Cú pháp để sử dụng Prototype là:

object.prototype.name = value

Ví dụ

Bạn thử ví dụ sau để sử dụng thuộc tính prototype để thêm một thuộc tính tới một đối tượng.

<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", "Mohtashim");
         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>

Kết quả

Book title is : Perl
Book author is : Mohtashim
Book price is : 100

Các bài học JavaScript khác tại s2sontech:




Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Learning English Everyday