1
“我有认识一块我的代码的问题:调用父原型法
//constructor
function Widget (options) {
};
//return the string
Widget.prototype._addEditFormString = function (val) {
return "<input type='text' value='" + val + "' >";
}
//initializing method
Widget.prototype.init = function() {
var addRowButton = document.getElementsByName("addRow")[0];
addRowButton.addEventListener("click", this.addRow, false);
};
//this context in this method still confusing me
Widget.prototype.addRow = function() {
console.log(this._addEditFormString);//Uncaught TypeError: Object #<HTMLInputElement> has no method '_addEditFormString'
}
var wid = new Widget();
wid.init();
问题 - 在init()方法我添加事件侦听器(addRow方法),但在addRow方法我不。知道如何捕捉我的构造类的“this”,因为我想调用_addEditFormString()方法,但接收到“未捕获的TypeError:Object [object Window]没有方法'_addEditFormString'”。我如何解决没有Widget.prototype ._addEditFormString?或者只有一个解决方案?谢谢。
感谢您的回答,但在addRow()中捕获“this”的问题会导致addEventListener在“this”中传递为BUTTON。如何调用Widget.prototype._addEditFormString()像this._addEditFormString()? – 2013-03-06 14:58:33
@ user1506183我不确定我是否收到你的问题,但是我编辑了我的答案。 – 2013-03-06 15:00:36
是的。得到这个,非常感谢函数(){_ this.addRow()}解决我的问题) – 2013-03-06 15:03:01