ES5继承
function Person(name, age){this.name = namethis.age = age}function Men(name,age){Person.call(this,name,age)//改变this 指针,并且继承它拥有的属性// this是有new Men 实例来的,实由子给父。}Person.prototype.say = function(){console.log('hello')}Men.prototype = Object.create(Person.prototype)//继承Person原型上的属性、方法。 Men.prototype._ proto_ = Person.prototypeMen.prototype.constructor = Men// 再次指向自己,知道为啥要再指向自己么?const men = new Men('lilei',18)men instanceof Men // trueinstanceof作用就是检查后面Men.prototype 是否在前面对象的原型上出现过。出现过返回truemen.__proto__ === Men.protype // truemen.__proto__.__proto__ === Person.prototype // true
ES6继承
// 定义父类class Person {constructor(name, age){this.name = name;this.age = age;}say(){console.log('hello')}}// 定义子类class Men extends Person { // 子类继承父类constructor(name, age){super(name, age); // 相当于Person.call(this,name,age)}}// 省略了子类的prototype来自父类的prototype// 省略了子类的构造函数再指向自己var men = new Men('jane',18);
继承中this 产生?class 对象是否可遍历? class 会提升?对象属性拦截??
es6 this 是由父类产生的,子类需要在conscturce 里调用super()子类才会拥有this
class 不存在变量提升,需要先定义再使用。
对象属性拦截 ? Proxy
