总结出JavaScript有以下几种继承继的方法:
1.对象冒充方法,可以多承继;
如:
A=function()
{
this.code="001";
this.name="whiteangell";
this.getCode=function()
{
return this.code;
}
this.getName=function()
{
return this.name
}
};
B = function()
{
this.newMethod=A;
this.newMethod();
this.age=20;
this.getAge=function()
{
return this.age;
}
};
var bb = new B();
alert(bb.getName());
2.原型链法;
如:
A=function()
{
this.code="001";
this.name="whiteangell";
this.getCode=function()
{
return this.code;
}
this.getName=function()
{
return this.name
}
};
B = function()
{
this.age=20;
this.getAge=function()
{
return this.age;
}
};
B.prototype=new A();
var bb = new B();
alert(bb.getName());
3.拷贝复制法
如:
Object.extend = function(destination,source)
{
for ( pro in source )
{
destination [pro] = source [pro];
}
return destination ;
};
A = function(){};
A.prototype =
{
code:"001",
name:"whiteangell",
getCode:function()
{
return this.code;
},
getName:function()
{
return this.name
}
};
B= function(){};
B.prototype= Object.extend({
age:20,
getAge:function()
{
return this.age;
}
},A.prototype);
var bb = new B();
alert(bb.getName());
4.call()方法,可以多承继;
如:
A=function()
{
this.code="001";
this.name="whiteangell";
this.getCode=function()
{
return this.code;
}
this.getName=function()
{
return this.name
}
};
B = function()
{
A.call(this);
this.age=20;
this.getAge=function()
{
return this.age;
}
};
var bb = new B();
alert(bb.getName());
5.apply()方法,可以多承继;
如:
A=function()
{
this.code="001";
this.name="whiteangell";
this.getCode=function()
{
return this.code;
}
this.getName=function()
{
return this.name
}
};
B = function()
{
A.apply(this);
this.age=20;
this.getAge=function()
{
return this.age;
}
};
var bb = new B();
alert(bb.getName());
分享到:
相关推荐
javascript的几种继承方法介绍_.docx
JavaScript中的几种继承方法示例 1.原型链继承 原理: 子类原型指向父类实例对象实现原型共享,即Son.prototype = new Father()。 这里先简单介绍下原型 js中每个对象都有一个__proto__属性,这个属性指向的...
NULL 博文链接:https://raychase.iteye.com/blog/1337415
Javascript中的几种继承方式对比分析_.docx
【JavaScript的9种继承实现方式归纳】js实现继承的几种方式.pdf
下面小编就为大家带来一篇javascript的几种继承方法介绍。小编觉得挺不错的。现在分享给大家,给大家一个参考
这种说法原因一般都是觉得javascript作为一门弱类型语言与类似java或c#之类的强型语言的继承方式有很大的区别,因而默认它就是非主流的面向对象方式,甚至竟有很多书将其描述为’非完全面向对象’语言。其实个人觉得...
本文给大家简单介绍了下如何在javascript中实现继承的几种方法,十分的实用,有需要的小伙伴可以参考下。
1.原型链继承 原理: 子类原型指向父类实例对象实现原型共享,即Son.prototype = new Father()。 这里先简单介绍下原型 js中每个对象都有一个__proto__属性,这个属性指向的就是该对象的原型。js中每个函数都有一个...
主要介绍了Javascript编程中几种继承方式比较分析,较为详细的分析了JavaScript继承的原理并对比分析了几种继承方式的实现技巧,需要的朋友可以参考下
深入JS继承 目录 前言准备总结继承的n种方式原型式继承原型链式继承借用构造函数(类式继承...撇开ES6 class不谈,传统的继承方式你知道几种?每种实现原理是什么,优劣点能谈谈吗。这里就结合具体例子,按照渐进式的
在javascript中,方法可以通过以下几种方式执行: 1.func(),这是最直接最常见的调用方式,也符合一般人的思维逻辑,但是在某些情况下有一些不足,下面会解释。 2.(function(arg){})(window),匿名方法调用,在构造...
javascript 中的继承实例...下面介绍几种js的继承: 原型链继承 原型链继承实现的本质是重写原型对象,代之以一个新类型的实例。代码如下: function SuperType() { this.property = true; } SuperType.prototype.
我们看看JavaScript中定义类的各种方法。 1.工厂方式 javaScript中创建自己的类和对象,我们应该是必须掌握的,我们都知道javaScript中对象的属性可以在对象创建后动态定义,比如下面的代码: 代码如下:[removed]...
主要介绍了详述JavaScript实现继承的几种方式(推荐)的相关资料,需要的朋友可以参考下
通过call()、apply()和bind()方法,我们可轻易地借用其它对象的方法,而无须从这些对象中继承它。 在JavaScript中借用方法 在JavaScript中,有时可以重用其它对象的函数或方法,而不一定非得是对象本身或原型上定义...
学完了Javascript类和对象的创建之后,现在总结一下Javascript继承...有以下几种方法: 1、对象冒充 [removed] function classA(str){ this.str=str; this.printstr=function(){ [removed](this.str); docume