JavaScript Es6 Class 'extends' 松散抽象 class 方法
JavaScript Es6 Class 'extends' looses abstract class methods
我是 ES6 的新手,正在重构代码以使用继承,但遇到了这个我不明白的错误。
请有人帮忙解释一下,子 class 添加到数组后无法访问父 class 的方法:
class abstract1 {
constructor(){
}
method1_2(){
console.log('method1_2');
}
method1_1(){
console.log('method1_1');
this.method1_2();
}
}
class test extends abstract1{
constructor(){
super();
}
method2_1(){
console.log('method2_1');
this.method1_1();
}
}
let tmp = new test();
tmp.method2_1();
//All good with world, result:
// method2_1
// method1_1
// method1_2
console.log('Now Run from a callback');
let eventActions = [];
eventActions.push({test:tmp.method2_1});
eventActions[0].test();
//Not good with world:
// method2_1
// Uncaught TypeError: this.method1_1 is not a function
// at Object.method2_1 [as test] (<anonymous>:22:8)
// at <anonymous>:35:17
您未接来电 bind:
eventActions.push({test:tmp.method2_1.bind(tmp)});
我是 ES6 的新手,正在重构代码以使用继承,但遇到了这个我不明白的错误。
请有人帮忙解释一下,子 class 添加到数组后无法访问父 class 的方法:
class abstract1 {
constructor(){
}
method1_2(){
console.log('method1_2');
}
method1_1(){
console.log('method1_1');
this.method1_2();
}
}
class test extends abstract1{
constructor(){
super();
}
method2_1(){
console.log('method2_1');
this.method1_1();
}
}
let tmp = new test();
tmp.method2_1();
//All good with world, result:
// method2_1
// method1_1
// method1_2
console.log('Now Run from a callback');
let eventActions = [];
eventActions.push({test:tmp.method2_1});
eventActions[0].test();
//Not good with world:
// method2_1
// Uncaught TypeError: this.method1_1 is not a function
// at Object.method2_1 [as test] (<anonymous>:22:8)
// at <anonymous>:35:17
您未接来电 bind:
eventActions.push({test:tmp.method2_1.bind(tmp)});