闭包中使用this
来源:互联网 发布:体育废 知乎 编辑:程序博客网 时间:2024/05/01 08:11
1.var name="the window";var object={ name:"my object", getNameFunc:function(){ return function(){ return this.name; }; }}alert(object.getNameFunc()())//无括号是返回function(){return function(){return this.name;//一个括号返回function(){return this.name;//两个括号返回the window
2.var name="the window";var object={ name:"my object", getNameFunc:function(){
var that=this; return function(){ return that.name; }; }}alert(object.getNameFunc()())
3.var name="the window";var object={name:"my object",getNameFunc:function(){return this;};}}alert(object.getNameFunc())
//SyntaxError: Unexpected token ;报错
//字面量里面多了一个分号,去掉后返回object object