<!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 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 = this, 和直接写that = this形式上一样,但是this己经是全局对象