我在Firefox 3.6.11運行測試,如果該事項,和eval在通話和應用的上下文中行爲不端。它以某種方式跳過目前的'this'對象。爲什麼?如何JavaScript的EVAL手柄「這個」
dojo.provide("yal-js.tests.javascript");
function evaltest() {
var dis=this;
// it works now... returns 2 on call and apply
return eval("(function() {return this.testValue;}).call(dis);");
// this, however, didn't work: it returned 1, not 2
//return eval("(function() {return this.testValue;})();");
}
function controltest() {
return this.testValue;
}
var testValue=1;
var testObj={testValue: 2};
doh.register("tests.javascript",
new TFRunGroup(
["direct",
function() {doh.assertEqual(1,controltest());} ],
["call",
function() {doh.assertEqual(2, controltest.call(testObj));}],
["apply",
function() {doh.assertEqual(2, controltest.apply(testObj));}],
["eval direct",
function() {doh.assertEqual(1,evaltest());} ],
["eval call",
function() {doh.assertEqual(2, evaltest.call(testObj));}],
["eval apply",
function() {doh.assertEqual(2, evaltest.apply(testObj));}]
));
@Dmitri:也許他認爲'eval'是不是你的代碼的問題。並且請與試圖幫助你的人一起更禮貌。 – Chetan 2010-10-27 05:33:09