We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原型:
原型链: 对于js对象都有一个内置的原型(proto)属性,当访问一个对象的属性或方法时,先从对象自身搜索,如果找不到,就从原型对象(proto)上搜索,该原型对象上也有自己的原型对象(proto),依次层层向上搜索,直到找到匹配的属性或方法返回, 如果找不到返回null,由原型(proto)属性构成的依次向上层原型对象进行搜索的链条就是原型链;原型链是实现继承的主要方法.
构造函数,原型对象,实例对象的关系如图:
// 定义父类构造函数 function Animal() { this.name = '动物' this.colors = ['black', 'white', 'gray'] } Animal.prototype.getName = function() { return this.name } // 定义子类构造函数 function Cat() { this.language = '喵喵' } // 重写子类的原型对象 Cat.prototype = new Animal() Cat.prototype.getLanguage = function() { return this.language } let cat1 = new Cat() cat1.colors.push('yellow') // 动物 console.log(cat1.getName(),'cat1 getname') // 喵喵 console.log(cat1.getLanguage()) // ["black", "white", "gray", "yellow"] console.log(cat1.colors, 'cat1') let cat2 = new Cat() // ["black", "white", "gray", "yellow"] console.log(cat2.colors, 'cat2')
function Animal() { this.name = '动物' this.colors = ['black', 'white', 'gray'] } Animal.prototype.getName = function() { return this.name } function Cat() { this.language = '喵喵' // 复制父类的实例给子类 Animal.call(this) } Cat.prototype.getLanguage = function() { return this.language } let cat1 = new Cat() // TypeError: cat1.getName is not a function console.log(cat1.getName(),'cat1 getname') // 喵喵 console.log(cat1.getLanguage()) cat1.colors.push('yellow') console.log(cat1.colors, 'cat1') // ['black', 'white', 'gray','yellow'] let cat2 = new Cat() console.log(cat2.colors, 'cat2') // ['black', 'white', 'gray']
function Animal() { this.name = '动物' this.colors = ['black', 'white', 'gray'] } Animal.prototype.getName = function() { return this.name } function Cat() { this.language = '喵喵' // 复制父类的实例给子类 Animal.call(this) } // 重写子类的原型对象 Cat.prototype = new Animal() // or 优化 Cat.prototype = Animal.prototype
let Animal = { name: '动物', colors: ['black', 'white', 'gray'] } let cat1 = Object.create(Animal) let cat2 = Object.create(Animal) // 动物 console.log(cat1.name, 'cat1') // 动物 console.log(cat2.name, 'cat2') cat1.colors.push('yellow') // ["black", "white", "gray", "yellow"] console.log(cat1.colors, 'cat1') // ["black", "white", "gray", "yellow"] console.log(cat2.colors, 'cat2')
function Cat(target) { let cloneObj = Object.create(target) clone.language = function() { console.log('喵喵') } return cloneObj } let Animal = { name: '动物', colors: ['black', 'white', 'gray'] } let cat1 = new Cat(Animal) console.log(cat1.name) cat1.language()
function Animal() { this.name = '动物' this.colors = ['black', 'white', 'gray'] } Animal.prototype.getName = function() { return this.name } function Cat() { this.language = '喵喵' Animal.call(this) } // 创建父类原型的一个副本,主要与父类原型对象隔离 Cat.prototype = Object.create(Animal.prototype) // 重写原型后,修改constructor的指向 Cat.prototype.constructor = Cat let cat1 = new Cat() // true console.log(cat1 instanceof Cat) // true console.log(cat1 instanceof Animal) // Animal {} console.log(cat1.__proto__) // Cat {} console.log(cat1.constructor)
class Animal { constructor() { this.name = '动物' this.colors = ['black', 'white', 'gray'] } } class Cat extends Animal { constructor() { super() this.language = '喵喵' } } let cat1 = new Cat() console.log(cat1.name)
一篇文章理解JS继承——原型链/构造函数/组合/原型式/寄生式/寄生组合/Class extends
再谈javascriptjs原型与原型链及继承相关问题
The text was updated successfully, but these errors were encountered:
No branches or pull requests
1. 相关概念及继承分类
原型:
原型链: 对于js对象都有一个内置的原型(proto)属性,当访问一个对象的属性或方法时,先从对象自身搜索,如果找不到,就从原型对象(proto)上搜索,该原型对象上也有自己的原型对象(proto),依次层层向上搜索,直到找到匹配的属性或方法返回, 如果找不到返回null,由原型(proto)属性构成的依次向上层原型对象进行搜索的链条就是原型链;原型链是实现继承的主要方法.
构造函数,原型对象,实例对象的关系如图:
2. JS实现继承的方法
2.1 原型链继承: 重写原型对象,即使用父类的实例重写子类的原型对象
分析
2.2 构造函数继承:复制父类的实例给子类
分析:
2.3 组合继承:原型链继承和构造函数继承的结合,借用原型链实现对父类原型对象的属性和方法的继承,借用构造函数实现对父类实例属性和方法的继承
分析:
2.4 原型式继承:让新对象的原型对象指向参数对象
分析:
2.5 寄生式继承:使用原型式继承获得参数对象的浅复制,然后增强这个对象
分析:
2.6 寄生组合继承:解决组合继承会两次调用父类构造函数的问题;完美的一种继承方式
分析:
2.7 ES6 class extends: 寄生组合继承的语法糖
分析:
引用参考文章
一篇文章理解JS继承——原型链/构造函数/组合/原型式/寄生式/寄生组合/Class extends
再谈javascriptjs原型与原型链及继承相关问题
The text was updated successfully, but these errors were encountered: