下面的脚本会产生“你好”,“不确定”,“你好”消息框:JavaScript函数回调
function action(callback) {
window.setTimeout(callback, 1000);
}
var obj = {
text: "Hello",
f: function() { window.alert(this.text); }
};
obj.f(); // Line 1
action(obj.f); // Line 2
action(function() { obj.f(); }); // Line 3
我找的解释,为什么行标记为“2号线”生产“未定义”输出。
''这个'主题的最佳答案:http://stackoverflow.com/questions/80084/in-javascript-why-is-the- this-operator-inconsistent/80478#80478 and http://stackoverflow.com/questions/541167/what-is-the-rationale-for-the-behavior-of-the-this-keyword-in-javascript/542060# 542060 – 2009-11-18 02:44:18
第一个引用真的很好 – LicenseQ 2009-11-18 02:53:20