javaScript中的继承
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javaScript中的继承,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3899字,纯文字阅读大概需要6分钟。
内容图文
![javaScript中的继承](/upload/InfoBanner/zyjiaocheng/602/e971becbf1c0488fa33d753f36f7ccc5.jpg)
借用原型链 通过prototype属性
function SuperType() { this.superType = 'SuperType'; } SuperType.prototype.getSuper = function() { return this.superType; } function ChildType() { this.childType = ';childType' } // 继承了SuperType ChildType.prototype = new SuperType(); let instance1 = new ChildType(); console.log(instance1.getSuper()); // 'SuperType'
//如果父类上也相同的方法或者属性,将会覆盖这个方法,但不影响父类方法或属性
借用构造函数 apply和call 优点可以传递参数
function SuperType() { this.colors = ['red', 'blue', 'green'] } function SubType() { // 继承SuperType SuperType.call(this) } var instance1 = new SubType() var instance2 = new SubType() instance1.colors.push('black') console.log(instance1.colors) // ["red", "blue", "green", "black"] console.log(instance2.colors) // ["red", "blue", "green"]
组合继承 原型链+构造函数
function SuperType(name) { this.name = name; this.colors = ['red', 'green', 'yellow']; } SuperType.prototype.getColors = function() { return this.colors; } function ChildType(name) { // 继承属性 SuperType.call(this, name); } // 继承方法 ChildType.prototype = new SuperType(); let instance1 = new ChildType('Nick'); let instance2 = new ChildType('Cherry'); instance1.colors.push('black'); console.log(instance1.name); // 'Nick' console.log(instance1.colors); // ' ["red", "green", "yellow", "black"]' instance1.getColors(); // ' ["red", "green", "yellow", "black"]' console.log(instance2.name); // 'Cherry' console.log(instance2.colors); // ["red", "green", "yellow"]
通过Object.create()与Object.defineProperties()方法的第二参数格式相同,
let person = { name: 'Nick', friends: ['cherry', 'july'], }; let person1 = Object.create(person); person1.name = 'Jhon'; person1.friends.push('cherry'); let person2 = Object.create(person); person2.name = 'Lily'; person2.friends.push('Bob'); console.log(person.friends); // ["cherry", "july", "cherry", "Bob"]
var person = { name: "Bob", friends: ["Shelby", "Court", "Van"] }; var anotherPerson = Object.create(person, { name: { value: "Greg" }, age: { value: 18 } }); alert(anotherPerson.name); //Greg alert(anotherPerson.age); //18 alert(person.name); //Bob
寄生式继承 创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后返回对象.
function createAnother(original){ var clone = Object.create(original); //通过调用函数创建一个新对象 clone.sayHi = function(){ //以某种方式来增强这个对象 alert("Hi"); }; return clone; //返回这个对象 } var person = { name: "Bob", friends: ["Shelby", "Court", "Van"] }; var anotherPerson = createAnother(person); anotherPerson.sayHi();
寄生组合式继承 都会调用两次构造函数:一次是在创建子类型原型时,另一次是在子类型构造函数内部。
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); //第二次调用SuperType() this.age = age; } SubType.prototype = new SuperType(); //第一次调用SuperType() SubType.prototype.sayAge = function(){ alert(this.age); }
function inheritPrototype(subType, superType){ var protoType = Object.create(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; } inheritPrototype(SubType, SuperType) SubType.prototype.sayAge = function(){ alert(this.age); } var instance = new SubType("Bob", 18); instance.sayName(); instance.sayAge();
inheritPrototype函数接收两个参数:子类型构造函数和超类型构造函数。
1. 创建超类型原型的副本。
2. 为创建的副本添加constructor属性,弥补因重写原型而失去的默认的constructor属性
3. 将新创建的对象(即副本)赋值给子类型的原型
这种方法只调用了一次SuperType构造函数,instanceof 和isPrototypeOf()也能正常使用。
内容总结
以上是互联网集市为您收集整理的javaScript中的继承全部内容,希望文章能够帮你解决javaScript中的继承所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。