一種基於ES5的JavaScript繼承

2021-09-08 10:16:41 字數 1120 閱讀 6942

//exports function.prototype.extends

//exports global.getprototypenames

//基於es5的繼承實現函式

function.prototype.extends=(function(),{});

} /**

* 繼承乙個類。

* 若有興許類,則共享興許類截至到當前的快照屬性(constructor除外),

* 這些屬性中的getters,setters和methods須考慮到要是通用的(如array的那些methods)

**/function inherits(s)else

if(arguments.length>1));

} c.prototype=p;

} return inherits;

}());

//測試準備

//~~~~~~~~~~~~~~~~~~~~~~~~

// baselist extends array

//~~~~~~~~~~~~~~~~~~~~~~~~

function baselist()

baselist.prototype.add=function(e);

baselist.extends(array);

//~~~~~~~~~~~~~~~~~~~~~~~~

// itemlist extends baselist

//~~~~~~~~~~~~~~~~~~~~~~~~

function itemlist()

itemlist.extends(baselist,eventtarget);

itemlist.prototype.item=function item(index){

index>>>=0;

return index"));

list.push(document.documentelement);

list.push(document.head);

console.assert(list.item(1)===document.head,"the second item of list is document.head");

ES5 陣列的擴充套件

array.prototype.indexof value 得到值在陣列中的第乙個下標 array.prototype.lastindexof value 得到值在陣列中的最後乙個下標 array.prototype.foreach function item,index 遍歷陣列 array.pr...

es5的嚴格模式

es5是ecmascript的第五個公升級版本 在原有的js中增加了 1.嚴格模式 使用嚴格模式在當前 段的頂部新增字串 use strict use strict翻譯使用嚴格的 嚴格模式的4個新要求 1.禁止給未宣告的變數賦值 在非嚴格模式下沒有宣告的變數是會自動在全域性建立該變數的,在嚴格模式下...

ES5陣列的方法

map 對映 語法 陣列.map function item,index,arr item 陣列中每乙個資料 index 索引 arr 原始陣列 foreach 迴圈 陣列.foreach function item,index,arr filter 過濾器 陣列.filter function i...