JavaScript的继承
1.调用基类的构造函数
function classA(sColor) { this.color = sColor; this.sayColor = function() { alert(this.color); }; } function classB(sColor,sName) { this.newMethod = classA; this.newMethod(sColor); delete this.newMethod; this.name = sName; this.sayName = function() { alert(this.name); }; } /*var objA = new classA('red'); var objB = new classB('blue','classb'); objA.sayColor(); objB.sayColor(); objB.sayName();*/
2.使用apply,call函数
function classC(sColor) { this.color = sColor; this.sayColor = function() { alert(this.color); }; } function classD(sColor,sName) { //classC.call(this,sColor); classC.apply(this,new Array(sColor)); this.name = sName; this.sayName = function() { alert(this.name); }; } /*var objC = new classC('redc'); var objD = new classD('blued','classd'); objC.sayColor(); objD.sayColor(); objD.sayName();*/
3.原型继承
function classE(sColor) { this.color = sColor; } classE.prototype.sayColor = function() { alert(this.color); }; function classF(sColor,sName) { classE.call(this,sColor); this.name = sName; } classF.prototype = new classE(); classF.prototype.sayName = function() { alert(this.name); }; /*var objE = new classE('red2'); var objF = new classF('blue2','classf'); objE.sayColor(); objF.sayColor(); objF.sayName(); if(objF instanceof classE) alert('objF instanceof classE');*/
相关推荐
nmgxzm00 2020-11-10
ifconfig 2020-10-14
hhanbj 2020-11-17
zfszhangyuan 2020-11-16
古叶峰 2020-11-16
一个智障 2020-11-15
jipengx 2020-11-12
81427005 2020-11-11
xixixi 2020-11-11
游走的豚鼠君 2020-11-10
苗疆三刀的随手记 2020-11-10
Web卓不凡 2020-11-03
小飞侠V 2020-11-02
帕尼尼 2020-10-30
爱读书的旅行者 2020-10-26
帕尼尼 2020-10-23
杏仁技术站 2020-10-23
淼寒儿 2020-10-22