javascript eval 如何处理“this”?

发布于 2024-09-29 05:31:17 字数 1147 浏览 0 评论 0原文

我在 Firefox 3.6.11 中运行了测试,如果这很重要的话,并且 eval 在调用和应用的上下文中行为不当。它以某种方式跳过当前的“this”对象。为什么?

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) );}]
        ));

I ran tests in Firefox 3.6.11, if that matters, and eval misbehaves in the context of call and apply. It somehow jumps over the current 'this' object. Why?

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) );}]
        ));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤千羽 2024-10-06 05:31:17

this 在 javascript 中是一个调用函数的对象,
当您使用 object.function() 时,thisobject
当您使用 function.call(object,...) 时,thisobject
当您使用 function.apply(object,...) 时,this 是对象,
当您使用new constructor(...)时,this新构造的对象
否则this是浏览器中的全局对象,它是window

this at javascript is an object that called function,
when you use object.function() then this is object,
when you use function.call(object,...) then this is object,
when you use function.apply(object,...) then this is object,
when you use new constructor(...) then this is new constructed object,
otherwise this is the global object in brower it is window.

勿挽旧人 2024-10-06 05:31:17

call 接受 this 的值作为其第一个参数(请参阅 此处)。因此,在第一行中,

return eval("(function() {return this.testValue;}).call(dis);");

您将传递给它的 dis ,它指向 evaltest,因此 this.testValue 指向 evaltest.testValue

在不起作用的第二行中,

return eval("(function() {return this.testValue;})();");

您将 null 传递给 this,因此 this.testValue 设置为窗口的 this.testValue 对象。

call takes in, as its first argument, the value of this (see here). So in the first line that works

return eval("(function() {return this.testValue;}).call(dis);");

you are passing it dis which points to evaltest, so this.testValue points to evaltest.testValue.

In the second line that doesn't work

return eval("(function() {return this.testValue;})();");

you are passing it null for this, so this.testValue is set to the window's this.testValue object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文