码迷,mamicode.com
首页 > 其他好文 > 详细

(0, eval)("this")

时间:2017-03-25 17:51:50      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:getname   var   his   ret   htm   win   返回   this   cti   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
var name = "The window";
var object1 = {
name: "My object",
getNameFunc: function(){
var that = (0,eval)("this"); //返回"the window
// var that = this; //返回"My object"
return function(){
// that = this;
return that.name;
};
}
};
alert(object1.getNameFunc()());
</script>
</head>
<body>

</body>
</html>

(0, eval)逗号表达式返回值再调用函数,将引发this值指向全局对象。所以that = (0, eval)("this"),表面上结果为that = this,和直接that = this的形式一样,但是此时的this并非指向object1,而是指向window

(0, eval)("this")

标签:getname   var   his   ret   htm   win   返回   this   cti   

原文地址:http://www.cnblogs.com/gumptious/p/6617594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!