我使用原型的独立的类继承:https://github.com/Jakobo/PTClass类的继承与原型的类接口
和我有以下类:
App.hello = Class.create({
initialize: function(args) {
this.name = args.name
},
sayHello: function() {
console.log('Hello, ' + this.name);
},
sayGoodbye: function() {
console.log('Goodbye, ' + this.name);
}
});
App.yo = Class.create(App.hello, {
initialize: function($super) {
$super();
},
sayHello: function() {
console.log('Yo, ' + this.name);
}
});
凡想法是,yo
将从hello
继承并覆盖其sayHello
方法。但也可以在其父类中调用sayGoodbye
方法。
所以我打电话给他们,像这样:
var test = new App.hello({name: 'Cameron'});
test.sayHello();
test.sayGoodbye();
var test2 = new App.yo({name: 'Cameron'});
test2.sayHello();
test2.sayGoodbye();
但是我得到的错误Uncaught TypeError: Cannot read property 'name' of undefined
我yo
类。
我该如何正确继承我的hello
类?
只是把它那里:PrototypeJS的'Class'东西是过时,PrototypeJS是不是真的保持多少这些天。您最好学习ES2015中引入的新“类”语法(又名“ES6”),并根据需要为旧版浏览器进行转译。 –