首页 > 代码库 > (0, eval)("this")
(0, eval)("this")
<!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")