Javascript this object

I have been working on a web project in the last 4 months. To optimize code performance, we used a template. I doubt if this really improves performance or not?

when we need to use an object this, we assign it to a local variable and use it.

function someFunction()
{
  var thisObject = this;
  //use thisObject in all following the code. 
}

Assuming that assigning an object to a thislocal stack variable improves performance.

I have not seen this type of encoding, so I doubt that it is useless.

EDIT: I know that assigning this object to a local variable is done to save the object, but this is not our case.

+5
source share
3 answers

Javascript, . this this , .

function someFunction() {
  var thisObject = this;
  var someCallback = function() {
    console.log(thisObject === this);  // Could print true or false
  };
  return someCallback;
}

, thisObject === this true, ,

var o = {} 
o.someFunction = someFunction();
var callback = o.someFunction();
callback();        // prints false
callback.call(o);  // prints true
+7

, , . ( ), :

enter image description here

Chrome Firefox , . IE9 , this, self, .

Chrome Firefox IE9, this , , , , , .

this , , , this - , , .

SO , , , this , , .

+2

() , , , .

.

However, in the end you should measure, not guess.

0
source

All Articles