Understanding "this" in Javascript - Stack Overflow

I have 2 blocks of code, one that does not work, and one that works because I assign that = this and us

I have 2 blocks of code, one that does not work, and one that works because I assign that = this and use that in my function instead of this. Can someone help me understand why this is so. It would help to know how one should think of accessing variables in functions in objects in JavaScript, and the nature of "this", if I am saying that right (if not, please enlighten me). Thank you!

var add = function (x, y) {
  return x + y;
  }

var myObject = {
  value: 0,
  increment: function (inc) {
    this.value += typeof inc === 'number' ? inc : 1;
    }
};

myObject.double2 = function () {
  // var that = this; 

  var helper = function () {
    this.value = add(this.value, this.value)
  };

  helper();
};

myObject.increment(100);
document.writeln(myObject.value); // Prints 100
myObject.double2();
document.writeln('<BR/>'); // Prints <BR/>
document.writeln(myObject.value); // Prints 100, **FAILS**

And the modified code:

var add = function (x, y) {
  return x + y;
  }

var myObject = {
  value: 0,
  increment: function (inc) {
    this.value += typeof inc === 'number' ? inc : 1;
    }
};

myObject.double2 = function () {
  var that = this;  

  var helper = function () {
    that.value = add(that.value, that.value)
  };

  helper();
};

myObject.increment(100);
document.writeln(myObject.value); // Prints 100
myObject.double2();
document.writeln('<BR/>'); // Prints <BR/>
document.writeln(myObject.value); // Prints 200 - **NOW IT WORKS**

I have 2 blocks of code, one that does not work, and one that works because I assign that = this and use that in my function instead of this. Can someone help me understand why this is so. It would help to know how one should think of accessing variables in functions in objects in JavaScript, and the nature of "this", if I am saying that right (if not, please enlighten me). Thank you!

var add = function (x, y) {
  return x + y;
  }

var myObject = {
  value: 0,
  increment: function (inc) {
    this.value += typeof inc === 'number' ? inc : 1;
    }
};

myObject.double2 = function () {
  // var that = this; 

  var helper = function () {
    this.value = add(this.value, this.value)
  };

  helper();
};

myObject.increment(100);
document.writeln(myObject.value); // Prints 100
myObject.double2();
document.writeln('<BR/>'); // Prints <BR/>
document.writeln(myObject.value); // Prints 100, **FAILS**

And the modified code:

var add = function (x, y) {
  return x + y;
  }

var myObject = {
  value: 0,
  increment: function (inc) {
    this.value += typeof inc === 'number' ? inc : 1;
    }
};

myObject.double2 = function () {
  var that = this;  

  var helper = function () {
    that.value = add(that.value, that.value)
  };

  helper();
};

myObject.increment(100);
document.writeln(myObject.value); // Prints 100
myObject.double2();
document.writeln('<BR/>'); // Prints <BR/>
document.writeln(myObject.value); // Prints 200 - **NOW IT WORKS**
Share Improve this question asked Jan 11, 2012 at 3:43 zallarakzallarak 5,5258 gold badges44 silver badges54 bronze badges 1
  • this refers to the local execution context. (I am 100% sure someone else can word that better then me.) Your that variable is a closure, that captures the value of the this so that you can reference it from inside the function call, whenever it happens, at some point in the future. – asawyer Commented Jan 11, 2012 at 3:49
Add a ment  | 

4 Answers 4

Reset to default 4

The first one doesn't work because when each function's this depends on how it was called.

First you do myObject.double2() and so this = myObject. But inside double2, you call helper() by itself and there is no object you're calling it under (it's not myObject.helper()). So this defaults to the global object (or window object in a browser).

In the second example, you "capture" a reference to myObject (that=this=myObject) and so that.value=myObject.value.

I think this link would greatly help you in understanding the differences of object and private members in Javascript to tackle your problem, please take a look at the Private section. Hope it helps!

Mozilla has some good reading on this. If you want this to work without assigning this to that, you can always use call.

Example: jsfiddle/5azde/

And you can always remember this:

When a function of an object is called, the object will be passed as this value(as the case 'add' and 'increment' function which belong to 'window' and 'myObject' separately). And if the function doesn't belong to any objects, window(or global) will be passed as this value.(as the case with function helper in your sample code).

And I'm glad to see a pure js question. No jQuery, No css, No dom selector. haha.

May it helps. :-)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742350585a4427433.html

相关推荐

  • Understanding &quot;this&quot; in Javascript - Stack Overflow

    I have 2 blocks of code, one that does not work, and one that works because I assign that = this and us

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信