Javascript 扩展可手持设备
我正在尝试创建一个包装器,以提供一些附加功能。我尝试了以下操作,虽然构造函数可以工作,但loadData函数似乎没有被重写。有什么建议吗 我已经在Chrome 45.0.2454.101m上测试过了Javascript 扩展可手持设备,javascript,handsontable,Javascript,Handsontable,我正在尝试创建一个包装器,以提供一些附加功能。我尝试了以下操作,虽然构造函数可以工作,但loadData函数似乎没有被重写。有什么建议吗 我已经在Chrome 45.0.2454.101m上测试过了 "use strict"; class CustomHandsontable extends Handsontable { constructor(container, options) { console.log("in constructor"); s
"use strict";
class CustomHandsontable extends Handsontable {
constructor(container, options) {
console.log("in constructor");
super(container, options);
}
loadData(data) {
console.log("load data");
super.loadData(data);
}
}
编辑:在了解到Chrome还没有完全支持ECMA6之后,我尝试了以下方法,但不太清楚如何调用父loadData方法
function CustomHandsontable (container, options) {
console.log("constructor");
this.loadData = function(data) {
console.log("loadData");
// TODO how do you call the parent load data function?
}
};
CustomHandsontable.prototype = Object.create(Handsontable.prototype);
CustomHandsontable.prototype.constructor = CustomHandsontable;
编辑:按照@ZekeDroids提示使用Babel,我在尝试调用super classes loadData函数时遇到以下错误:
Uncaught TypeError: Cannot read property 'call' of undefined
:
“严格使用”;
var_createClass=(function(){function defineProperties(target,props){for(var i=0;i
回答我自己的问题
我无法让继承发挥作用,但确实找到了使用组合的解决方案:
function Hot(container, options, customOptions) {
var _instance = this;
// create a hot instance
var hot = new Handsontable(container, options);
hot._hotContainer = this;
//
// put any custom stuff here...
//
//
// since inheritance doesn't quite work, the following are all pass-through
// functions to cover the fact that composition is being used
//
this.loadData = function(data) {
hot.loadData.apply(this, arguments);
}
this.render = function() {
hot.render.apply(this, arguments);
}
this.getDataAtRow = function() {
return hot.getDataAtRow.apply(this, arguments);
}
this.countCols = function() {
return hot.countCols.apply(this, arguments);
}
this.sort = function() {
hot.sort.apply(this, arguments);
}
};
您是否尝试过使用Babel将您的ES6代码转换为ES5?这是当今编写代码的一种相当标准的方式。这是可取的,因为Handsontable已经采用了ES6和ES7编码转换方式。@ZekeDroid:谢谢您提供有关Babel的提示。我尝试过,但出现了一个错误(请参阅我更新的帖子).是的,我不知道该怎么做。只是好奇,你真的需要扩展Handsontable吗?你可以阅读他们关于如何创建自定义版本的文档,否则,我发现几乎没有任何情况下正常的数据操作和JS不符合他们的目的
function Hot(container, options, customOptions) {
var _instance = this;
// create a hot instance
var hot = new Handsontable(container, options);
hot._hotContainer = this;
//
// put any custom stuff here...
//
//
// since inheritance doesn't quite work, the following are all pass-through
// functions to cover the fact that composition is being used
//
this.loadData = function(data) {
hot.loadData.apply(this, arguments);
}
this.render = function() {
hot.render.apply(this, arguments);
}
this.getDataAtRow = function() {
return hot.getDataAtRow.apply(this, arguments);
}
this.countCols = function() {
return hot.countCols.apply(this, arguments);
}
this.sort = function() {
hot.sort.apply(this, arguments);
}
};