本文摘自PHP中文网,作者不言,侵删。
本篇文章给大家带来的内容是关于javascript现继承的四种方式(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、原型链继承
核心: 将父类的实例作为子类的原型
缺点: 父类新增原型方法/原型属性,子类都能访问到,父类一变其它的都变了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function Person (name) {
this.name = name;
};
Person.prototype.getName = function () {
return this.name;
};
function Parent (age) {
this.age = age;
};
Parent.prototype = new Person( '老明' );
Parent.prototype.getAge = function () {
return this.age;
};
var result = new Parent(22);
console.log(result.getName());
console.log(result.getAge());
|
2、构造继承
基本思想
借用构造函数的基本思想就是利用call
或者apply
把父类中通过this
指定的属性和方法复制(借用)到子类创建的实例中。
因为this
对象是在运行时基于函数的执行环境绑定的。也就是说,在全局中,this
等于window
,而当函数被作为某个对象的方法调用时,this
等于那个对象。
call
、apply
方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。
所以,这个借用构造函数就是,new
对象的时候(new创建的时候,this
指向创建的这个实例),创建了一个新的实例对象,
并且执行Parent
里面的代码,而Parent
里面用call
调用了Person
,也就是说把this
指向改成了指向新的实例,
所以就会把Person
里面的this
相关属性和方法赋值到新的实例上,而不是赋值到Person
上面,
所以所有实例中就拥有了父类定义的这些this
的属性和方法。
因为属性是绑定到this
上面的,所以调用的时候才赋到相应的实例中,各个实例的值就不会互相影响了。
核心:使用父类的构造函数来增强子类实例,等于是复制父类的实例属性给子类(没用到原型)
缺点: 方法都在构造函数中定义, 只能继承父类的实例属性和方法,不能继承原型属性/方法,无法实现函数复用,每个子类都有父类实例函数的副本,影响性能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function Person (name) {
this.name = name;
this.friends = [ '小李' , '小红' ];
this.getName = function () {
return this.name;
}
};
function Parent = (age) {
Person.call(this, '老明' );
this.age = age;
};
var result = new Parent(23);
console.log(result.name);
console.log(result.friends);
console.log(result.getName());
console.log(result.age);
console.log(result.getSex());
|
3、组合继承
组合继承(所有的实例都能拥有自己的属性,并且可以使用相同的方法,组合继承避免了原型链和借用构造函数的缺陷,结合了两个的优点,是最常用的继承方式)
核心:通过调用父类构造,继承父类的属性并保留传参的优点,然后再通过将父类实例作为子类原型,实现函数复用
缺点:调用了两次父类构造函数,生成了两份实例(子类实例将子类原型上的那份屏蔽了)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function Person (name) {
this.name = name;
this.friends = [ '小李' , '小红' ];
};
Person.prototype.getName = function () {
return this.name;
};
function Parent (age) {
Person.call(this, '老明' );
this.age = age;
};
Parent.prototype = new Person( '老明' );
var result = new Parent(24);
console.log(result.name);
result.friends.push( "小智" );
console.log(result.friends);
console.log(result.getName());
console.log(result.age);
var result1 = new Parent(25);
console.log(result1.name);
console.log(result1.friends);
|
4、寄生组合继承
核心:通过寄生方式,砍掉父类的实例属性,这样,在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点
缺点:堪称完美,但实现较为复杂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function Person(name) {
this.name = name;
this.friends = [ '小李' , '小红' ];
}
Person.prototype.getName = function () {
return this.name;
};
function Parent(age) {
Person.call(this, "老明" );
this.age = age;
}
( function () {
var Super = function () {};
Super.prototype = Person.prototype;
Parent.prototype = new Super();
})();
var result = new Parent(23);
console.log(result.name);
console.log(result.friends);
console.log(result.getName());
console.log(result.age);
|
【相关推荐:JavaScript视频教程】
以上就是javascript现继承的四种方式(代码示例)的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
js中20个常用字符串方法及使用方式(总结)
javascript数组删除的方法有哪些
ajax和javascript之间有什么区别
javascript数组排序方法是什么
javascript数组怎么删除指定元素
javascript如何将object转为数组
javascript如何定义方法
详解使用 javascript 解析 url的方法
javascript使用什么标签实现
javascript如何删除数组
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript现继承的四种方式(代码示例)