js this 闭包
Posted 连冰华
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js this 闭包相关的知识,希望对你有一定的参考价值。
var myObject = { value :0, increment:function (inc){ this.value += typeof inc ===‘number‘ ? inc:1; } }; myObject .increment(); console.log(myObject.value); myObject .increment(2); console.log(myObject.value); var a=1; var b={ a:2, c:function(){ console.log(this.a); } } b.c(); (b.c)(); (0,b.c)(); function f1(){ var n=999; nAdd=function(){n+=1} console.log(n); function f2(){ alert(n); } return f2; } var result=f1(); result(); nAdd(); result(); var name = "The Window"; var object = { name : "My Object", getNameFunc : function(){ return function(){ return this.name; }; } }; alert(object.getNameFunc()()); var name = "The Window"; var object = { name : "My Object", getNameFunc : function(){ var that = this; return function(){ return that.name; }; } }; alert(object.getNameFunc()());
以上是关于js this 闭包的主要内容,如果未能解决你的问题,请参考以下文章
关于js里的this指向,函数的prototype,闭包理解