0
我有一個關於以下代碼打印出「this.text」的問題。如何獲得回調在類範圍內使用「this」
我需要一個包裝功能,使其工作。這太麻煩了。
有沒有一種更簡單的方法(沒有額外的包裝)讓它工作?
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)
謝謝!有效 – 2013-02-24 05:30:12