码迷,mamicode.com
首页 > Web开发 > 详细

js this 闭包

时间:2016-03-29 14:32:02      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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 闭包

标签:

原文地址:http://www.cnblogs.com/xiaotaiyang/p/5332601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!