努力了解新的ES6语法。如果我有一个像Outlayer库这样的类,它不是用ES6编写的,但是想用ES6类扩展它,那会怎么样?关键的扩展点是_getItemLayoutPosition
我试过这样的事情。
export let MyGrid = Outlayer.create('AnyGrid', {});
MyGrid._getItemLayoutPosition = function() {
// this does not get called
}
如果我new MyGrid(element)
我的扩展_getItemLayoutPosition永远不会被调用。
那么我想也许我需要扩展课程
export class AnyGrid extends Outlayer {
_getItemLayoutPosition(item) {
return {
x: 0,
y: 0
}
}
}
这会产生错误Type 'typeof 'Outlayer'' is not a constructor function type.
扩展此类的正确方法是什么,以便我可以覆盖_getItemLayoutPosition
方法?
答案 0 :(得分:0)
请尝试以下方法:
// AnyGrid- subclass
function AnyGrid() {
Outlayer.apply(this, arguments); // call super constructor.
}
// subclass extends superclass
AnyGrid.prototype = Object.create(Outlayer.prototype);
AnyGrid.prototype.constructor = AnyGrid;
//Add/Override the method
AnyGrid.prototype._getItemLayoutPosition = function() {
// this does not get called
}
不要忘记导出AnyGrid
。
答案 1 :(得分:0)
由于Outlayer.create
实际上创建了一个继承自Outlayer
的函数,你必须在原型上添加方法而不是函数本身
export let MyGrid = Outlayer.create('AnyGrid', {});
MyGrid.prototype._getItemLayoutPosition = function() {
// this does not get called
}
请注意,当您致电Outlayer.create
has additional properties时返回的功能,即与创建继承自Outlayer