Javascript String prototype 属性

返回 Javascript String 对象


定义

Javascript String prototype 属性允许您向任何对象(数字、布尔值、字符串和日期等)添加属性和方法。

注意- Prototype 是一个全局属性,几乎适用于所有对象。

语法

语法如下:

object.prototype.name = value

浏览器支持

Internet Explorer Firefox Opera Google Chrome Safari

所有主流浏览器都支持 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", "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

尝试一下


返回 Javascript String 对象

查看笔记

扫码一下
查看教程更方便