javascript原型函数被忽略
我正在吃晚饭 “无法读取未定义的属性“bind” 我的javascript对象中的问题。如何强制执行绑定 调用此函数时会发生这种情况:javascript原型函数被忽略,javascript,prototype-programming,Javascript,Prototype Programming,我正在吃晚饭 “无法读取未定义的属性“bind” 我的javascript对象中的问题。如何强制执行绑定 调用此函数时会发生这种情况: function ViewportDatasource(mockServer) { this.mockServer = mockServer; this.connectionId =this.mockServer.connect(this.eventListener.bind(this)); } 即使在原
function ViewportDatasource(mockServer) {
this.mockServer = mockServer;
this.connectionId =this.mockServer.connect(this.eventListener.bind(this));
}
即使在原型函数中正确定义了eventListener
ViewportDatasource.prototype.eventListener = function (event) {
switch (event.eventType) {
case 'rowCountChanged':
this.onRowCountChanged(event);
break;
case 'rowData':
this.onRowData(event);
break;
case 'dataUpdated':
this.onDataUpdated(event);
break;
}
};
我能做什么
为了回答Daniel的问题,将在此处调用该实例:
function setRowData($http) {
// set up a mock server - real code will not do this, it will contact your
// real server to get what it needs
var mockServer = new MockServer();
$http.get('data.json').then(function(response){
mockServer.init(response.data);
});
var viewportDatasource = new ViewportDatasource(mockServer);
table.api.setViewportDatasource(viewportDatasource);
// put the 'size cols to fit' into a timeout, so that the scroll is taken into consideration
setTimeout(function () {
table.api.sizeColumnsToFit();
}, 100);
}
调用方中的ag grid对象准备就绪后执行此函数
function ViewportDatasource(mockServer) {
//the this operator here is undefined. function is not of object.
this.mockServer = mockServer;
this.connectionId =this.mockServer.connect(this.eventListener.bind(this));
}
只有将函数绑定到对象时,才应使用this运算符,在这种情况下,this运算符才有意义。在上面的例子中没有。您是如何构造实例的?什么时候?我怎样才能纠正它?那么,我如何引用对象本身呢?