Javascript-object-prototype

提供:Dev Guides
移動先:案内検索

JavaScriptオブジェクト-プロトタイプ

説明

prototypeプロパティを使用すると、プロパティとメソッドを任意のオブジェクト(数値、ブール値、文字列、日付など)に追加できます。

注意-プロトタイプは、ほぼすべてのオブジェクトで使用できるグローバルプロパティです。

構文

その構文は次のとおりです-

object.prototype.name = value

次の例を試してください。

<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>

出力

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