prototype 的說明 js類

來源:互聯網
上載者:User

如下是其中關於 prototype 的說明:

=====
constructor 屬性

應用於: Array 對象 | Boolean 對象 | Date 對象 | Function 對象 | Number 對象 | Object 對象 | String 對象
要求
版本 2
返回物件類型原型的引用。

objectName.prototype
objectName 參數是對象的名稱。

說明
用 prototype 屬性提供對象的類的一組準系統。對象的新執行個體“繼承”賦予該對象原型的操作。

例如,要為 Array 對象添加返回數組中最大元素值的方法。要完成這一點,聲明該函數,將它加入 Array.prototype,並使用它。

function array_max( ){
var i, max = this[0];
for (i = 1; i < this.length; i++)
{
if (max < this[i])
max = this[i];
}
return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max( );
該代碼執行後,y 儲存數組 x 中的最大值,或說 6。

所有 JScript 內部對象都有唯讀 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.