javascript eval 如何处理“this”?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
this 在 javascript 中是一个调用函数的对象,
当您使用
object.function()
时,this
是object
,当您使用
function.call(object,...)
时,this
是object
,当您使用
function.apply(object,...)
时,this
是对象,当您使用
new constructor(...)
时,this
是新构造的对象
,否则
this
是浏览器中的全局对象
,它是window
。this at javascript is an object that called function,
when you use
object.function()
thenthis
isobject
,when you use
function.call(object,...)
thenthis
isobject
,when you use
function.apply(object,...)
thenthis
is object,when you use
new constructor(...)
thenthis
isnew constructed object
,otherwise
this
isthe global object
in brower it iswindow
.call
接受this
的值作为其第一个参数(请参阅 此处)。因此,在第一行中,您将传递给它的
dis
,它指向evaltest
,因此this.testValue
指向evaltest.testValue
。在不起作用的第二行中,
您将
null
传递给this
,因此this.testValue
设置为窗口的this.testValue
对象。call
takes in, as its first argument, the value ofthis
(see here). So in the first line that worksyou are passing it
dis
which points toevaltest
, sothis.testValue
points toevaltest.testValue
.In the second line that doesn't work
you are passing it
null
forthis
, sothis.testValue
is set to the window'sthis.testValue
object.