导航:首页 > 方法技巧 > js如何继承一个方法

js如何继承一个方法

发布时间:2022-04-02 18:45:46

⑴ JavaScript中继承应该怎么实现

1.原型链

基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法。

构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。

原型链实现继承例子:

function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function subType() {
this.property = false;
}
//继承了SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function (){
return this.property;
}
var instance = new SubType();
console.log(instance.getSuperValue());//true

2.借用构造函数

基本思想:在子类型构造函数的内部调用超类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。

例子:

function SuperType() {
this.colors = ["red","blue","green"];
}
function SubType() {
SuperType.call(this);//继承了SuperType
}
var instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors);//"red","blue","green","black"
var instance2 = new SubType();
console.log(instance2.colors);//"red","blue","green"

3.组合继承

基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。

例子:

function SuperType(name) {
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
function SubType(name, age) {
SuperType.call(this,name);//继承属性
this.age = age;
}
//继承方法
SubType.prototype = new SuperType();
Subtype.prototype.constructor = Subtype;
Subtype.prototype.sayAge = function() {
console.log(this.age);
}
var instance1 = new SubType("EvanChen",18);
instance1.colors.push("black");
consol.log(instance1.colors);//"red","blue","green","black"
instance1.sayName();//"EvanChen"
instance1.sayAge();//18
var instance2 = new SubType("EvanChen666",20);
console.log(instance2.colors);//"red","blue","green"
instance2.sayName();//"EvanChen666"
instance2.sayAge();//20

4.原型式继承

基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。

原型式继承的思想可用以下函数来说明:

function object(o) {
function F(){}
F.prototype = o;
return new F();
}

例子:

var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"

ECMAScript5通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和一个作为新对象定义额外属性的对象。

var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"

5.寄生式继承

基本思想:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真正是它做了所有工作一样返回对象。

例子:

function createAnother(original) {
var clone = object(original);
clone.sayHi = function () {
alert("hi");
};
return clone;
}
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();///"hi"

6.寄生组合式继承

基本思想:通过借用函数来继承属性,通过原型链的混成形式来继承方法

其基本模型如下所示:

function inheritProperty(subType, superType) {
var prototype = object(superType.prototype);//创建对象
prototype.constructor = subType;//增强对象
subType.prototype = prototype;//指定对象
}

例子:

function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function (){
alert(this.name);
};
function SubType(name,age){
SuperType.call(this,name);
this.age = age;
}
inheritProperty(SubType,SuperType);
SubType.prototype.sayAge = function() {
alert(this.age);
}

⑵ js中有哪些继承方式

继承的方式一共有三种:
一、原型继承
通过prototype 来实现继承。
function Person(name,age) { this.name=name; this.age=age;
}

Person.prototype.sayHello=function(){
alert (''使用原型得到Name:'' + this.name);

} var per = new Person("马小倩",21);
per.sayHello();//输出:使用原型得到Name:马小倩

function Student(){}

Student.prototype=new Person("洪如彤",21); //实现原型继承

var stu = new Student();

Student.prototype.grade=5;

Student.prototype.intr=function(){
alert(this.grade);
}

stu.sayHello();//输出:使用原型得到Name:洪如彤
stu.intr();//输出:5

二、构造函数实现继承
function Person(name,age) { this.name=name; this.age=age;
}

Person.prototype.sayHello=function(){
alert (''使用原型得到Name:'' + this.name);

} var per = new Person("马小倩",21);
per.sayHello();//输出:使用原型得到Name:马小倩

三、 通过call、apply 实现继承

⑶ js中继承的几种用法总结

一,js中对象继承

js中有三种继承方式

1.js原型(prototype)实现继承
复制代码 代码如下:
<SPAN style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-SIZE: 18px"><html>
<body>
<script type="text/javascript">
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.sayHello=function(){
alert("使用原型得到Name:"+this.name);
}
var per=new Person("马小倩",21);
per.sayHello(); //输出:使用原型得到Name:马小倩

function Student(){}
Student.prototype=new Person("洪如彤",21);
var stu=new Student();
Student.prototype.grade=5;
Student.prototype.intr=function(){
alert(this.grade);
}
stu.sayHello();//输出:使用原型得到Name:洪如彤
stu.intr();//输出:5
</script>
</body>
</html></SPAN></SPAN>

2.构造函数实现继承
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px"><html>
<body>
<script type="text/javascript">
function Parent(name){
this.name=name;
this.sayParent=function(){
alert("Parent:"+this.name);
}
}

function Child(name,age){
this.tempMethod=Parent;
this.tempMethod(name);
this.age=age;
this.sayChild=function(){
alert("Child:"+this.name+"age:"+this.age);
}
}

var parent=new Parent("江剑臣");
parent.sayParent(); //输出:“Parent:江剑臣”
var child=new Child("李鸣",24); //输出:“Child:李鸣 age:24”
child.sayChild();
</script>
</body>
</html></SPAN>

3.call , apply实现继承
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px"><html>
<body>
<script type="text/javascript">
function Person(name,age,love){
this.name=name;
this.age=age;
this.love=love;
this.say=function say(){
alert("姓名:"+name);
}
}

//call方式
function student(name,age){
Person.call(this,name,age);
}

//apply方式
function teacher(name,love){
Person.apply(this,[name,love]);
//Person.apply(this,arguments); //跟上句一样的效果,arguments
}

//call与aplly的异同:
//1,第一个参数this都一样,指当前对象
//2,第二个参数不一样:call的是一个个的参数列表;apply的是一个数组(arguments也可以)

var per=new Person("武凤楼",25,"魏荧屏"); //输出:“武凤楼”
per.say();
var stu=new student("曹玉",18);//输出:“曹玉”
stu.say();
var tea=new teacher("秦杰",16);//输出:“秦杰”
tea.say();

</script>
</body>
</html></SPAN>

二、call和apply的用法(详细介绍)

js中call和apply都可以实现继承,唯一的一点参数不同,func.call(func1,var1,var2,var3)对应的apply写法为:func.apply(func1,[var1,var2,var3])。

JS手册中对call的解释:
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px">call 方法
调用一个对象的一个方法,以另一个对象替换当前对象。

call([thisObj[,arg1[, arg2[, [,.argN]]]]])

参数
thisObj
可选项。将被用作当前对象的对象。

arg1, arg2, , argN
可选项。将被传递方法参数序列。

说明
call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。

如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。</SPAN>

说简单一点,这两函数的作用其实就是更改对象的内部指针,即改变对象的this指向的内容。这在面向对象的js编程过程中有时是很有用的。下面以apply为例,说说这两个函数在 js中的重要作用。如:
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px"> function Person(name,age){ //定义一个类
this.name=name; //名字
this.age=age; //年龄
this.sayhello=function(){alert(this.name)};
}
function Print(){ //显示类的属性
this.funcName="Print";
this.show=function(){
var msg=[];
for(var key in this){
if(typeof(this[key])!="function"){
msg.push([key,":",this[key]].join(""));
}
}
alert(msg.join(" "));
};
}
function Student(name,age,grade,school){ //学生类
Person.apply(this,arguments);//比call优越的地方
Print.apply(this,arguments);
this.grade=grade; //年级
this.school=school; //学校
}
var p1=new Person("卜开化",80);
p1.sayhello();
var s1=new Student("白云飞",40,9,"岳麓书院");
s1.show();
s1.sayhello();
alert(s1.funcName);</SPAN>

另外,Function.apply()在提升程序性能方面有着突出的作用:
我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所有参数中的最大值。
比如
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px">alert(Math.max(5,8)); //8
alert(Math.max(5,7,9,3,1,6)); //9

//但是在很多情况下,我们需要找出数组中最大的元素。

var arr=[5,7,9,1];
//alert(Math.max(arr)); // 这样却是不行的。NaN

//要这样写
function getMax(arr){
var arrLen=arr.length;
for(var i=0,ret=arr[0];i<arrLen;i++){
ret=Math.max(ret,arr[i]);
}
return ret;
}

alert(getMax(arr)); //9

//换用apply,可以这样写
function getMax2(arr){
return Math.max.apply(null,arr);
}

alert(getMax2(arr)); //9

//两段代码达到了同样的目的,但是getMax2却优雅,高效,简洁得多。

//再比如数组的push方法。
var arr1=[1,3,4];
var arr2=[3,4,5];
//如果我们要把 arr2展开,然后一个一个追加到arr1中去,最后让arr1=[1,3,4,3,4,5]
//arr1.push(arr2)显然是不行的。 因为这样做会得到[1,3,4,[3,4,5]]

//我们只能用一个循环去一个一个的push(当然也可以用arr1.concat(arr2),但是concat方法并不改变arr1本身)

var arrLen=arr2.length;
for(var i=0;i<arrLen;i++){
arr1.push(arr2[i]);
}

//自从有了Apply,事情就变得如此简单

Array.prototype.push.apply(arr1,arr2); //现在arr1就是想要的结果</SPAN>

⑷ js写一个方法,用于实现继承功能.怎么写

function Parent(firstname)
{
this.fname=firstname;
this.age=40;
this.sayAge=function()
{
console.log(this.age);
}
}
function Child(firstname)
{
this.parent=Parent;
this.parent(firstname);
delete this.parent;
this.saySomeThing=function()
{
console.log(this.fname);
this.sayAge();
}
}
var mychild=new Child("李");
mychild.saySomeThing();

⑸ 一个JS如何继承在不同文件中的另一个JS

由于javascript的类是源于function实现的,而这个function即代表类又代表了此类的构造函数。
var
a=function(b){}是定义在类中的一个函数。
prototype属性是用于继承类,如果有一个类为a,另一个类为b,如想b类继承a类,那么必须在b类中的prototype属性设置为a,即prototype=a;

⑹ 关于javascript的子类方法如何继承全部父类的方法(有特殊条件)

看你这个样子是有点强迫症,放到里面是没有问题的,至于你说的开销,你完全是想太多。因为你这个有构造函数参数,不能使用常规的方法。至于你为什么报错,是因为Animal.apply(this,arguments)并没有将Animal附加到Cat的原型链上,而你的move是在Animal的原型链上。你加一句:Cat.prototype=Animal.prototype,可以达到你想要的效果,但这个开销问题,有太多区别吗?我没有测试,你可以测试一下,呵呵。

⑺ js实现继承的几种方法

《继承法》第二十三条 继承开始后,知道被继承人死亡的继承人应当及时通知其他继承人和遗嘱执行人。继承人中无人知道被继承人死亡或者知道被继承人死亡而不能通知的,由被继承人生前所在单位或者住所地的居民委员会、村民委员会负责通知。第二十四条 存有遗产的人,应当妥善保管遗产,任何人不得侵吞或者争抢。第二十五条 继承开始后,继承人放弃继承的,应当在遗产处理前,作出放弃继承的表示。没有表示的,视为接受继承。受遗赠人应当在知道受遗赠后两个月内,作出接受或者放弃受遗赠的表示,到期没有表示的,视为放弃受遗赠。第二十六条 夫妻在婚姻关系存续期间所得的共同所有的财产,除有约定的以外,如果分割遗产,应当先将共同所有的财产的一半分出为配偶所有,其余的为被继承人的遗产。遗产在家庭共有财产之中的,遗产分割时,应当先分出他人的财产。第二十七条 有下列情形之一的,遗产中的有关部分按照法定继承办理:遗嘱继承人放弃继承或者受遗赠人放弃受遗赠的;遗嘱继承人丧失继承权的;遗嘱继承人、受遗赠人先于遗嘱人死亡的;遗嘱无效部分所涉及的遗产;遗嘱未处分的遗产。第二十八条 遗产分割时,应当保留胎儿的继承份额。胎儿出生时是死体的,保留的份额按照法定继承办理。第二十九条 遗产分割应当有利于生产和生活需要,不损害遗产的效用。不宜分割的遗产,可以采取折价、适当补偿或者共有等方法处理。第三十条 夫妻一方死亡后另一方再婚的,有权处分所继承的财产,任何人不得干涉。第三十一条 公民可以与扶养人签订遗赠扶养协议。按照协议,扶养人承担该公民生养死葬的义务,享有受遗赠的权利。公民可以与集体所有制组织签订遗赠扶养协议。按照协议,集体所有制组织承担该公民生养死葬的义务,享有受遗赠的权利。第三十二条无人继承又无人受遗赠的遗产,归国家所有;死者生前是集体所有制组织成员的,归所在集体所有制组织所有。第三十三条 继承遗产应当清偿被继承人依法应当缴纳的税款和债务,缴纳税款和清偿债务以他的遗产实际价值为限。超过遗产实际价值部分,继承人自愿偿还的不在此限。继承人放弃继承的,对被继承人依法应当缴纳的税款和债务可以不负偿还责任。第三十四条 执行遗赠不得妨碍清偿遗赠人依法应当缴纳的税款和债务。

⑻ js继承的几种方式 各

js继承用的还是比较少的,一般通过原型链继承或混合继承目的就是降低创建对象的开销!

各种继承示例如下:

构造函数继承:


<script>
//把父类方法放到其原型链中而非类声明体中,以便每次实例化子类时不至于都执行函数而增加创建对象的开销
Person.prototype.speakP=function(){
console.log("I'maPerson!");
};
functionPerson(age,name){
this.age=age;
this.name=name;
}
functionStudent(sno,age,name){
Person.call(this,age,name);
this.sno=sno;
}
vars=newStudent(95001,23,"小红");
console.log(Student.prototype.constructor);
//s.speakP();//s不是Person类型,所以不能调用speakP函数;如果非要调用,可使用以下方案二:原型链继承
</script>

原型链继承:


<script>
Person.prototype.speakP=function(){
console.log("I'maPerson!age="+this.age+"name="+this.name);//通过实例化的子类对象调用我时获取不到父类的age、name
};
functionPerson(age,name){
this.age=age;
this.name=name;
}
Student.prototype=newPerson();
//因为修改了Student原型链,需要把其原型链上构造器重新指向自己
Student.prototype.constructor=Student;
functionStudent(sno,age,name){
this.sno=sno;
}
vars=newStudent(95001,23,"小红");
console.log(Student.prototype.constructor);
s.speakP();
</script>

混合继承:


<script>
Person.prototype.speakP=function(){
console.log("I'maPerson!age="+this.age+"name="+this.name);
};
functionPerson(age,name){
this.age=age;
this.name=name;
}
Student.prototype=newPerson();
Student.prototype.constructor=Student;
functionStudent(sno,age,name){
Person.call(this,age,name);
this.sno=sno;
}
vars=newStudent(95001,23,"小红");
console.log(Student.prototype.constructor);
s.speakP();
</script>

ES6 extends继承:


<script>
classPerson{
constructor(age,name){
this.age=age;
this.name=name;
}
sayHi(){
console.log("I'maPerson!age="+this.age+"name="+this.name);
}
}
classStudentextendsPerson{
constructor(sno,age,name){
super(age,name);
this.sno=sno;
}
}
vars=newStudent(95001,24,"小红");
console.log(Student.prototype.constructor);
s.sayHi();
</script>

⑼ javascript中如何实现类的继承啊

js继承有5种实现方式:
1、继承第一种方式:对象冒充
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
//通过以下3行实现将Parent的属性和方法追加到Child中,从而实现继承
//第一步:this.method是作为一个临时的属性,并且指向Parent所指向的对象,
//第二步:执行this.method方法,即执行Parent所指向的对象函数
//第三步:销毁this.method属性,即此时Child就已经拥有了Parent的所有属性和方法
this.method = Parent;
this.method(username);//最关键的一行
delete this.method;

this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();

2、继承第二种方式:call()方法方式
call方法是Function类中的方法
call方法的第一个参数的值赋值给类(即方法)中出现的this
call方法的第二个参数开始依次赋值给类(即方法)所接受的参数

function test(str){
alert(this.name + " " + str);
}
var object = new Object();
object.name = "zhangsan";
test.call(object,"langsin");//此时,第一个参数值object传递给了test类(即方法)中出现的this,而第二个参数"langsin"则赋值给了test类(即方法)的str

function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.call(this,username);

this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();

3、继承的第三种方式:apply()方法方式
apply方法接受2个参数,
A、第一个参数与call方法的第一个参数一样,即赋值给类(即方法)中出现的this
B、第二个参数为数组类型,这个数组中的每个元素依次赋值给类(即方法)所接受的参数

function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.apply(this,new Array(username));

this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();

4、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承
function Person(){
}
Person.prototype.hello = "hello";
Person.prototype.sayHello = function(){
alert(this.hello);
}

function Child(){
}
Child.prototype = new Person();//这行的作用是:将Parent中将所有通过prototype追加的属性和方法都追加到Child,从而实现了继承
Child.prototype.world = "world";
Child.prototype.sayWorld = function(){
alert(this.world);
}

var c = new Child();
c.sayHello();
c.sayWorld();

5、继承的第五种方式:混合方式
混合了call方式、原型链方式

function Parent(hello){
this.hello = hello;
}
Parent.prototype.sayHello = function(){
alert(this.hello);
}

function Child(hello,world){
Parent.call(this,hello);//将父类的属性继承过来
this.world = world;//新增一些属性
}

Child.prototype = new Parent();//将父类的方法继承过来

Child.prototype.sayWorld = function(){//新增一些方法
alert(this.world);
}

var c = new Child("zhangsan","lisi");
c.sayHello();
c.sayWorld();

⑽ 关于JS实现继承的方法都有哪一些

定义一个父类:
// 定义一个动物类
function Animal (name) {
// 属性
this.name = name || ‘Animal’;
// 实例方法
this.sleep = function(){
console.log(this.name + ‘正在睡觉!’);
}
}
// 原型方法
Animal.prototype.eat = function(food) {
console.log(this.name + ‘正在吃:’ + food);
1.原型链继承
核心:将父类的实例作为子类的原型
function Cat(){
}
Cat.prototype = new Animal();
Cat.prototype.name = ‘cat’;

//Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.eat(‘fish’));
console.log(cat.sleep());
console.log(cat instanceof Animal); //true
console.log(cat instanceof Cat); //true
特点:
1.非常纯粹的继承关系,实例是子类的实例,也是父类的实例
2.父类新增的原型方法、属性,子类都能访问到
3.简单,易于实现
缺点:
1.要想为子类新增属性和方法,必须要在new Animal()这样的语句之后执行
(可以在cat构造函数中,为Cat实例增加实例属性)
2.无法实现多继承
3.来自原型对象的引用属性被所有实例共享
4.创建子类实例时,无法向父类构造函数传参
下面代码解释缺点3(注意是引用属性):
function Super(){
this.val = 1;
this.arr = [1];
}
function Sub(){
// ...
}
Sub.prototype = new Super(); // 核心
var sub1 = new Sub();
var sub2 = new Sub();
sub1.val = 2;
sub1.arr.push(2);
alert(sub1.val); // 2
alert(sub2.val); // 1
alert(sub1.arr); // 1, 2
alert(sub2.arr); // 1, 2

2.构造继承
核心:使用父类的构建函数来增强子类实例,等于复制父类的实例属性给子类(没用到原型),除了call方法,也可以用apply()
function Cat(name){
Animal.call(this);
this.name = name || ‘Tom’;
}
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true
特点:
1.解决了1中,子类实例共享父类引用属性的问题
2.创建子类实例时,可以向父类传递参数
3.可以实现多继承(call多个父类对象)
缺点:
1.实例并不是父类的实例,只是子类的实例
2.只能继承父类的实例属性和方法,不能继承原型属性和方法
3.无法实现函数复用,每个子类都有父类的实例函数的副本,影响性能

3.实例继承
核心:为父类实例添加新特性,作为子类实例返回
function Cat(name){
var instance = new Animal();
instance.name = name || ‘Tom’;
return instance;
}
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // false
特点:
1.不限制调用方式,不管是new 子类()还是子类(),返回的对象都具有相同的效果
缺点:
1.实例是父类的实例,不是子类的实例
2.不支持多继承

4. 拷贝继承
核心:使用for…in将父类实例中的方法赋给子类实例
unction Cat(name){
var animal = new Animal();
for(var p in animal){
Cat.prototype[p] = animal[p];
}
Cat.prototype.name = name || ‘Tom’;
}

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true
特点:
1.支持多继承
缺点:
1.效率较低,内存占用高(因为要拷贝父类的属性)
2.无法获取父类不可枚举的方法(for in无法访问不可枚举的方法)

5.组合继承
核心:通过调用父类构造,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用
function Cat(name){
Animal.call(this);
this.name = name || ‘Tom’;
}
Cat.prototype = new Animal();

//组合继承需要修复构造函数的指向
Cat.prototype.constructor=Cat;
// Test Code

var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // true
特点:
1.弥补了方式2的缺陷,可以继承实例属性、方法,也可以继承原型属性、方法
2.既是子类的实例,也是父类的实例
3.不存在引用属性的共享问题
4.可传参
5.函数可复用
缺点:
1.调用了两次父类构造函数,生成了两份实例(子类实例将子类原型上的那份屏蔽了)

6.寄生组合继承
核心:通过寄生方式,砍掉父类的实例属性,这样,在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点
function Cat(name){
Animal.call(this);
this.name = name || ‘Tom’;
}
(function(){
// 创建一个没有实例方法的类
var Super = function(){};
Super.prototype = Animal.prototype;
//将实例作为子类的原型
Cat.prototype = new Super();
//寄生组合继承需要修复构造函数的指向
Cat.prototype.constructor=Cat;
})();

// Test Code

var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true
特点:
1.堪称完美
缺点:
1.实现较为复杂 (BY三人行慕课)

阅读全文

与js如何继承一个方法相关的资料

热点内容
菜饼堆肥制作方法视频 浏览:120
成果解决教学问题的方法如何写 浏览:899
太阳能光电板安装方法 浏览:445
城市人口增多的问题和解决方法 浏览:270
人物形象的方法技巧 浏览:425
真假海螺肉的鉴别方法 浏览:772
胡子有点黄用什么方法洗 浏览:246
安卓手机亮屏时间哪里设置方法 浏览:358
激光笔的使用方法 浏览:625
架接果树方法视频 浏览:685
名师阅读教学方法 浏览:759
长发快速剪发方法视频 浏览:74
铜丝球连接方法 浏览:42
驳口金油使用方法 浏览:858
足背伸的锻炼方法 浏览:794
牛舍风机安装方法 浏览:393
道路绿地覆盖率的计算方法 浏览:766
做木珠子最简单的方法 浏览:396
灯光控制手机的使用方法 浏览:83
目的基因检测的步骤和方法是什么 浏览:678