【发布时间】:2017-03-03 02:16:48
【问题描述】:
我有一个关于打印“this.text”的代码的问题。
我需要一个包装函数来使它工作。这太麻烦了。
有没有更简单的方法(没有额外的包装器)让它工作?
function Class1() {
this.text = "test";
}
Class1.prototype.show = function() {
console.log(this);
console.log(this.text);
}
var testClass = new Class1();
function funWithCallBack(cb) {
cb();
}
// it will show "undefined" because "this" scope changes to window
funWithCallBack(testClass.show);
function wrapper() {
testClass.show();
}
// this one will work but troublesome
funWithCallBack(wrapper)
【问题讨论】:
标签: javascript callback