the this keyword in javascript

原文见:http://stackoverflow.com/questions/133973/how-does-this-keyword-work-within-a-javascript-object-literal

 

Cannibalized from another post of mine, here's more than you ever wanted to know about  this .

Before I start, here's the most important thing to keep in mind about Javascript, and to repeat to yourself when it doesn't make sense. Javascript does not have classes. If something looks like a class, it's a clever trick. Javascript has  objects   and  functions . (that's not 100% accurate, functions are just objects, but it can sometimes be helpful to think of them as separate things)

The  this   variable is attached to functions. Whenever you invoke a function,  this   is given a certain value, depending on how you invoke the function. This is often called the invocation pattern.

There are four ways to invoke functions in javascript. You can invoke the function as a  method , as afunction , as a  constructor , and with  apply .

As a Method

A method is a function that's attached to an object

var foo = {};
foo.someMethod = function(){
    alert(this);
}

When invoked as a method,  this   will be bound to the object the function/method is a part of. In this example, this will be bound to foo.

As A Function

If you have a stand alone function, the  this   variable will be bound to the "global" object, almost always thewindow   object in the context of a browser.

 var foo = function(){
    alert(this);
 }
 foo();

This may be what's tripping you up , but don't feel bad. Many people consider this a bad design decision. Since a callback is invoked as a function and not as a method, that's why you're seeing what appears to be inconsistent behavior.

Many people get around the problem by doing something like, um, this

var foo = {};
foo.someMethod = function (){
    var that=this;
    function bar(){
        alert(that);
    }
}

You define a variable  that   which points to  this . Closure (a topic all it's own) keeps  that   around, so if you call bar as a callback, it still has a reference.

As a Constructor

You can also invoke a function a a constructor. Based on the naming convention you're using (TestObject) this also  may be what you're doing and is what's tripping you up .

You invoke a function as a Constructor with the new keyword.

function Foo(){
    this.confusing = 'hell yeah';
}
var myObject = new Foo();

When invoked as a constructor, a new Object will be created, and  this   will be bound to that object. Again, if you have inner functions and they're used as callbacks, you'll be invoking them as functions, and  this will be bound to the global object. Use that var that = this trick/pattern.

Some people think the constructor/new keyword was a bone thrown to Java/traditional OOP programmers as a way to create something similar to classes.

With the Apply Method.

Finally, every function has a method (yes, functions are objects in Javascript) named "apply". Apply lets you determine what the value of  this   will be, and also lets you pass in an array of arguments. Here's a useless example.

function foo(a,b){
    alert(a);
    alert(b);
    alert(this);
}
var args = ['ah','be'];
foo.apply('omg',args);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值