JS的原型链、构造函数和组合继承是什么
Admin 2022-06-18 群英技术资讯 1100 次浏览
关于“JS的原型链、构造函数和组合继承是什么”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。首先学习继承之前,要对原型链有一定程度的了解。如果已经了解请继续。
父类实例作为子类的原型
子类创造的两个实例的隐式原型__proto__指向父类的那个实例
而父类的实例的隐式原型__proto__又指向父类的原型father.prototype
根据原型链的特性,所有子类的实例能够继承父类原型上的属性。
阅览以下这张图可以配合代码理解清晰:

//父类
function father() {
this.fatherAttr = ["fatherAttr"];
}
//父类的原型上的属性
father.prototype.checkProto = "checkProto";
//子类
function child() {}
// 将father实例作为child这个构造函数的原型
child.prototype = new father();
child.prototype.constructor = child;
//两个子类实例
const test1 = new child();
const test2 = new child();
console.log("测试1:");
console.log("test1:", test1);
console.log("test2:", test2);
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("测试2:");
test1.fatherAttr.push("newAttr");
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("测试3:");
console.log("test1.checkProto:", test1.checkProto);

特点:
fatherAttr属性,但是因为父类的实例会拥有fatherAttr属性,且现在父类的实例作为child的原型,根据原型链,他们可以共享到自己的构造函数child的原型上的属性。(测试1)fatherAttr,当原型上的属性作为引用类型时,此处是数组,test1添加一个新内容会导致test2上的fatherAttr也改变了。(测试2)(缺点)child构造函数不能传递入参。(缺点)将父类上的this绑定到子类,也就是当子类创造实例时会在子类内部调用父类的构造函数,父类上的属性会拷贝到子类实例上,所以实例会继承这些属性。
//父类
function father(params) {
this.fatherAttr = ["fatherAttr"];
this.params = params;
}
//父类的原型上的属性
father.prototype.checkProto = "checkProto";
//子类
function child(params) {
father.call(this, params);
}
//两个子类实例
const test1 = new child("params1");
const test2 = new child("params2");
console.log("测试1:");
console.log("test1:", test1);
console.log("test2:", test2);
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("测试2:");
test1.fatherAttr.push("newAttr");
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("测试3:");
console.log("test1.checkProto:", test1.checkProto);

特点:
fatherAttr属性,所以没有共享属性,创造一个实例就得拷贝一次父类的所有属性,且因为不能继承父类原型,所以方法不能复用,被迫拷贝方法。(测试1)(缺点)test1添加一个新内容只是改变了test1自己的属性,不会影响到test2。(测试2)child构造函数可以传递参数,定制自己的属性。(测试1)结合原型链继承和构造函数继承,可以根据两种继承特点进行使用。
//父类
function father(params) {
this.fatherAttr = ["fatherAttr"];
this.params = params;
}
//父类的原型上的属性
father.prototype.checkProto = "checkProto";
//子类
function child(params) {
//第二次调用了父类构造函数
father.call(this, params);
}
// 将father实例作为child构造函数的原型
child.prototype = new father();//第一次调用了父类构造函数
child.prototype.constructor = child;
//两个实例
const test1 = new child("params1");//从这里跳转去子类构造函数第二次调用了父类构造函数
const test2 = new child("params2");
console.log("测试1:");
console.log("test1:", test1);
console.log("test2:", test2);
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("测试2:");
test1.fatherAttr.push("newAttr");
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("测试3:");
console.log("test1.checkProto:", test1.checkProto);
console.log("测试4:");
delete test1.fatherAttr
console.log("test1:", test1);
console.log("test1.fatherAttr:", test1.fatherAttr);

特点:
fatherAttr属性,创造一个实例就得拷贝一次父类的所有属性(构造函数继承特点,测试1),但是能访问父类原型,可以把复用方法定义在父类原型上。(原型链继承特点,测试1)test1添加一个新内容只是改变了test1自己的属性,不会影响到test2。(构造函数继承特点,测试2)child构造函数可以传递参数,定制自己的属性。(构造函数继承特点,测试1)fatherAttr属性,实例仍然拥有隐式原型指向的父类实例上的fatherAttr属性。(原型链继承特点,测试4)(缺点)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了node.js 全局变量的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
jquery选定元素修改属性的方法:1、利用“$(指定元素)”语句获取指定元素对象;2、利用attr()方法来修改已获取到元素对象的属性 ,语法为“元素对象.attr(attribute,value)”。
这篇文章给大家介绍的是Vue中ref与toRef的区别,ref与toRef还是有一定的不同的,大家在使用时要注意,下文有详细的介绍,希望大家阅读完这篇能有收获,接下来一起跟随小编看看吧。
这篇文章我们来了解jQuery触发change事件的方法,手动触发改变事件我们需要使用到change()方法,接下来我们通过示例来了解change()方法的使用及触发改变事件的实现,感兴趣的朋友就继续往下看吧。
VUE+Element 前端应用实现国际化的处理还是非常方便的,一般在Main.js函数里面引入语言文件,然后在界面上进行一定的处理,把对应的键值转换为对应语言的语义即可。本篇随笔介绍在VUE+Element 前端应用中如何实现在界面快速的支持多语言国际化的处理逻辑代码。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008