var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
alert(object.getNameFunc()()); //The Window。谁知道这个为什么会打印this window吗?对闭包不是很熟悉,请教你们一下。为什么调用的时候是window这个对象来调用的呢?
object.getNameFunc()() === function(){return this.name}();
把函数名和函数功能分开来看。alert(object.getNameFunc()());
,函数功能是在全局作用域下实现的,所以指向window
。
确实和闭包无关
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
//如果在这里 return this.name;
//log ‘My Object’
//如果要在下面的function拿到‘My Object’
//就要用另一个变量将this传递给另一个执行环境
// that = this; 请看下面一段代码
return function(){
return this.name;
};
}
};
alert(object.getNameFunc()()); //The Window。
传递this
var name = "The Window";
var object = {
name: "My Object",
getNameFunc: function() {
that = this;
return function() {
return that.name;
};
}
};
console.log(object.getNameFunc()());
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(context){
var self = this; // object
return function(){
console.log(this); //window
console.log(self); //object
return this.name; //return window.name
};
}};
getNameFuct是挂载到了object这个对象上,所以通过self捕获到的this指向object,而getNameFuc内部的匿名嵌套函数并没有挂载到object上,而是属于全局对象window的,使用this时访问的是window
javascript
的 this
指向的是引用
这个问题很简单,就是this的指向问题,如果楼主用一个self变量来储存object,就可以了
var name = "The window";
var object={
name:"My Object",
getName: function(){
var self = this;
return function(){
return self.name;
}
}
}
console.log(object.getName()());
这个题目重点在于this的执行环境
obj.getNameFun()可以转换成
function() {
return this.name;
}
这个函数已经跟obj无关
而this是随着执行环境(局部,全局,apply,call,setTimeout)等,变化着它的指代的
所以,在全局范围运行的函数,this就是指向window
这个不是闭包的问题。object.getNameFunc()()
相当于
var temp = object.getNameFunc();
temp();
//所以this 是 window(如果不加 'use strict'的话)
看看这篇:一个js闭包问题的解答
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
var func = object.getNameFunc();
alert(func());
说实话,不觉得有多经典………………
另外这题也与闭包无关
《javascript高级程序设计》 182页,7.2.2关于this对象,就是这个程序。里面有一句很关键的话
匿名函数的执行环境具有全局性,因此其this对象通常指向window。
我在浏览器环境中跑了下结果的确如此,但是在node环境下结果是undefined,有人知道为什么么?
在 JavaScript 中, 上下文对象就是 this 指针,即被调用函数所处的环境。
没做特殊处理的话,this根据当前执行环境决定执行那个对象的
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
object.getNameFunc()只是函数的引用,函数体是
function (){
return this.name;
}
当调用这个函数 object.getNameFunc()() ,this指向的当然是 window
return function(){
return this.name;
};
return回去的方法,this指向的是window,除非:
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(context){
return function(){
return context.name;
};
}
};
alert(object.getNameFunc(object)());
此問題和閉包無關。
js 的作者 Brendan Eich 這樣設計,是爲了讓任何不是以 a.b()
形式調用的函數看上去像是 window 對象的方法:
也就是說,b()
等價於 window.b()
然而事實證明這一設計反直覺(其實 js 最初設計時沒有閉包,也就考慮不到閉包中的函數不該看作 window 的方法了)。
所以 ES6 新增了的 arrow function 可以按照題主的直覺執行了:
var name = "The Window";
var object = {
name: "My Object",
getNameFunc: function() {
return () => {
return this.name; // "this" is the same as getNameFunc
};
}
};