Javascript中object的this和that

因为Javascript中函数的上下文关键字this是如何调用函数决定的,所以函数的上下文this决不能包含为闭包的一部分。当需要在闭包内使用函数的上下文this的时候就需要转换成that。

先来一个简单的例子,匹配所有元素(jquery)然后alert,"need_show":

...

this.id='need_show';

var that=this;

$('*').each(function(){

  alert(that.id);

});

...

再从一个复杂的例子展开这个问题吧:

name = "The Window";
var object = {
  name : "My Object",
  getNameFunc : function(){
    return function(){
      return this.name;
    };
  }
};
alert(object.getNameFunc()());

上面的代码,很多时候大家都会搞错alert()的结果。

上面的代码,其实我们是希望,this能调用的是object内部属性"My Object",但第一种方法,却显示的是"The Window"。

在object的属性getNameFunc中使用this,是正确的。但大家注意,这个地方使用了一个return 匿名函数。也就是说,你在调用object.getNameFunc()(),这是其实相当于(function(){ return this.name; })(),这里的this当然就是window对象了。

解决方法,其实就是利用闭包,保存上下文this为that:

var object1 = {
  name : "My Object",
  getNameFunc : function(){
    var that=this;
    return function(){
      return that.name;
    };
  }
};
alert(object1.getNameFunc()());

这就是为什么要引入一个that,that其实是保存了object正确上下文this。再调用object.getNameFunc()()时,就能正确了(function(){ returnthat.name; })();

经常看JS书的人,常常可以看到这个例子。我开始也不知道为什么,这里要用that。因为闭包的原因,确实让JS更加丰富了,当然难度和可读性也更高了。

再举个很值得玩味一下的例子:

function a(){

  if ( !(this instanceof arguments.callee) )
    return new a();

}

有了这个例子,我们就可以在调用函数a时,如果a没有实例化就创建一个新实例时,不用使用new关键字了。同样也是靠闭包才实现的(函数访问自身)。确实,从这种角度看,JS中创建的函数,其实就是构造函数。


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object programming (OP) opens a new world of design possibilities that go far beyond inheritance. This book will help the intermediate JavaScript programmer learn to use both types of inheritance. For classical inheritance, it is accompanied by a substantial online system (a windowing UI library) that shows classical inheritance at its best. The same system shows how OP "capabilities" can eliminate much of the need for inheritance. For experienced JavaScript programmers, this book shows why most of the old views of JavaScript's inheritance have not done it justice. JavaScript classes inherit from JavaScript's prototypes, a fact that makes JavaScript's prototypes, when used correctly, functional equivalents to C++ classes (not to prototypes in true prototypical languages, like Self). JavaScript's object programming (not inheritance) is what separates it from classical OOP languages like C++ and Java. Most important, basing inheritance on JavaScript's prototypal chain is possible, but is not the best choice for prototypal inheritance or classical inheritance. What You'll Learn What are objects, JavaScript objects and object programming What is and how to use inheritance and JavaScript inheritance as well as inheritance alternatives How to design for JavaScript What are and how to use OO principles in JavaScript How to use Constructors with JavaScript and more Audience This book is for both intermediate and advanced JavaScript and Web development programmers. However, any programmer will understand the concepts and any JavaScript programmer should understand all of the concepts in this book. The code there is shows examples of the concepts discussed. Table of Contents Chapter 1: Creating Objects Chapter 2: Object Programming Chapter 3: Inheritance Theory Chapter 4: Inheritance Practice Chapter 5: On OOP Principles Chapter 6: More Ex Nihilo Objects Chapter 7: Inheritance Alternatives Chapter 8: Designing for JavaScript Chapter 9: On Constructors Chapter 10: Appendices

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值