1.ES6常用功能与常见问题分析-(原型链)

关于原型链:
原型链作为实现继承的主要方法,其基本思想是利用原型让一个引用类型继承另一个引用继承的属性和方法。
 
function A(name) {
var instance = this
this.name = name
//重写构造函数
A = function () {
return instance
}
A.prototype = this
// 直接指向旧的原型
A.prototype = this.constructor.prototype
instance = new A()
// 调整构造函数指针,这里实际上实现了一次原型链继承,如果不想这样实现,也可以直接指向原来的原型
instance.constructor = A
return instance
}

var a1 = new A()
var a2 = new A()


// ************************************


function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.lessons = ['Math', 'Physics'];
}
Person.prototype = {
constructor: Person, // 原型字面量方式会将对象的constructor变为Object,需要强制指回Person
getName: function () {
return this.name;
}
}

var person1 = new Person('Jack', 19, 'SoftWare Engneer');
person1.lessons.push('Biology');

var person2 = new Person('Lily', 39, 'Mechanical Engneer');

console.log(person1.lessons); // ["Math", "Physics", "Biology"]
console.log(person2.lessons); // ["Math", "Physics"]
console.log(person1.getName === person2.getName); // true


// ************************************

class A {
fun = () => {

}
}

class B extends A {
constructor(props) {
super(props);
}

fun1 = () => {

}
}

var a = new A()
posted @ 2019-04-13 15:00  web前端-张小七  阅读(153)  评论(0编辑  收藏  举报