ES5 中 JavaScript的繼承

2021-08-07 05:01:18 字數 1329 閱讀 2755

在es5 中,js 的繼承主要分為以下幾類:

類式繼承

function

ffatherclass

(){};

ffatherclass.prototype.falert = function

function

fchildclass

(){};

fchildclass.prototype = new ffatherclass();

fchildclass.prototype.fchildownfunc = function

()//因為是引用原型鏈的,所以缺點是乙個子類修改了父類的方法或屬性,那麼對其它子類也會產生影響

建構函式繼承

function

ffatherclass();

ffatherclass.prototype.falert = function

function

fchildclass

()//但是這樣繼承有乙個缺點,就是只能繼承父類在建構函式中的方法與屬性,不能繼承原型鏈上的方法與屬性

組合繼承

function

ffatherclass();

ffatherclass.prototype.falert = function

function

fchildclass

()fchildclass.prototype = new ffatherclass();

原型式繼承

var ofather = 

var ochild = inheritobject(ofather);

//然後就可以給子類新增自己的方法與屬性了

寄生式繼承

var ofather = 

var createchild(ofather)

return o;

}

寄生組合式繼承

function

ofatherclass

()ofatherclass.prototype.ofatherprototypefunction = function

(){};

function

ochildclass

()inheritprototype(ochildclass,ofatherclass);

ochildclass.prototype.childownchildfunction = function

(){};

JavaScript實現集合(ES5動態原型模式)

集合的概念 集合是由一組無序且唯一 即不能重複 的項組成的。這個資料結構使用了與有限集合相同的數學概念,但應用在電腦科學的資料結構中。你也可以把集合想象成乙個既沒有重複元素,也沒有順序概念的陣列。集合的結構 function set if typeof this add function set.p...

ES5中的新增方法

es5中給我們新增了一些方法,可以很方便的運算元組或者字串,這些方法主要包括 陣列方法 字串方法 物件方法 迭代 遍歷 方法 foreach map filter some every foreach array.foreach function currentvalue,index,arr cur...

es5中的保護物件

由物件中的屬性容易修改,js 在es5 增加了物件的屬性保護物件 用來合理的限制修改範圍 檢視這個小物件 var dongdong var nameobject object.getownpropertydescriptor dongdong,name console.log nameobject ...