prototype原型屬性

來源:互聯網
上載者:User

標籤:

 

<script type="text/javascript">/*需求:想把getMax與searchEle方法添加 到數組對象中。    functoin Array(){        this.prototype = new Object();                    this.getMax = function(){                }    }    Prototype注意的細節:    1.    prototype是函數(function)的一個必備屬性(書面一點的說法是"保留屬性")(只要是function,就一定有一個prototype屬性)    2.    prototype的值是一個對象    3.    可以任意修改函數的prototype屬性的值。    4.    一個對象會自動擁有prototype的所有成員屬性和方法。        */        Array.prototype.getMax = function(){        var max = this[0];        for(var index = 1; index<this.length ; index++){            if(this[index]>max){                max = this[index];                }            }        return max;    }            Array.prototype.searchEle = function(target){        for(var i = 0 ; i<this.length ; i++){            if(target==this[i]){                return i;                }            }        return -1;                }        //var arr = new Array(12,4,17,9);    var arr = [12,4,17,9];    var max = arr.getMax();    var index = arr.searchEle(9);    document.write("最大值:"+ max+"<br/>");    document.write("索引值:"+ index+"<br/>");</script>

 

prototype原型屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.