class Person { constructor(name, age) { this.name = name this.age = age } test() { }}class Student extends Person { constructor(name, age, no) { super(name, age) this.no = no } say() { console.log(`name: ${this.name}, age: ${this.age}, no: ${this.no}`) }}let student = new Student('mrcode', 21, '11403080435')student.say()student可以訪問test方法,這點可以理解。 但是為什么通過Student中的this可以訪問到父類中的name, age呢? ES6中的class只是原型鏈的語法糖。 原型鏈上的對象都是原型。 哪里來的name, age屬性呢?
JS中子類的實例屬性為什么可以訪問父類的實例屬性?
慕仙森
2018-10-19 17:11:34