javascript當中prototype用法

prototype
見上一節,馬克-to-win:prototype做用就是給某個類增長一個實例方法。

例 3.6.2

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
  /*When the Global object is created, it always has at least the following properties:
       Object object       Function object       Array object       String object
       Boolean object       Number object       Date object       Math object
       Value properties
   */
    Array.prototype.mymethod = function(number)
    {
        var result = -1;
/*注意mymethod功能是找出某數在數組中出現的位置。做爲Array的一個function,能夠訪問Array的屬性this.length, 參見上一個prototype的例子,   
    Student.prototype.infop = function()/*此方法能夠爲全部Student對象所用*/
    {
        document.writeln("對象的qixy屬性p:" + this.qixy);
        document.writeln("對象的age屬性p:" + this.age);
        /*下列話是合法的, 由於不是this.number, 而是Student.number*/
        document.writeln("static method is " + Student.number);
    };
*/
        for (var i = 0; i < this.length; i ++)
        {
            if (this[i] == number)
            {
                result = i;
                break;
            }
        }
        return result;
    }html

文章轉載自:https://blog.csdn.net/qq_44594249/article/details/100065974數組

相關文章
相關標籤/搜索