22

As said here:

http://jqfundamentals.com/book/index.html

Closures can also be used to resolve issues with the this keyword, which is unique to each scope. This mechanism can be particularly useful when dealing with callbacks, though in those cases, it is often better to use Function.bind, which will avoid any overhead associated with scope traversal.

But it doesn't really say how to distinguish between the two cases. I don't understand in fact what the author means by "avoid any overhead associated with scope traversal." Can you explain?

3
  • 5
    closure method seems to be twice faster jsperf.com/bind-vs-closure-performace Commented Sep 27, 2012 at 20:41
  • 3
    Link is broken. "Sorry, what you’re looking for isn’t here" Commented Jul 31, 2015 at 21:20
  • @sbr 25 x faster on my computer Commented Jan 7, 2016 at 12:11

3 Answers 3

11

What it's referring to are things like this

obj.doSomething = function() {
  var that = this;
  setTimeout(function() {
    // this is the window
    // that is the obj
    that.doSomethingElse();
  }, 50);
};

vs

obj.doSomething = function() {
  setTimeout((function() {
    // this is the obj
    this.doSomethingElse();
  }).bind(this), 50);
};

Benchmark. No noticable difference in chrome.

Sign up to request clarification or add additional context in comments.

8 Comments

Since function calls happen in microseconds and setTimeout is 10milliseconds+, isn't this lack of difference caused by setTimeout's low timing resolution?
Yes, but which of the two is considered best practice (ie. the "nicest to read")?
Using the this keyword is fundamental to object oriented programming. I would recommend against the suggestion given by @Raynos.
I find the first example nicest to read (...as long as a different variable name is used besides "that". that's confusing IMO.)
@Raynos, If I would like not not use this in javascript, is there a way to do that in the above example shown? I really don't like to use this in javascript but I don't find a way to do that.
|
10

Take a look at this line in the example in the link above

console.log(self.myName, this.myName);

(with self = this; a couple of lines above). The closure defined outerFunction method, exists in a different scope that is why it has a different this value from the outerObj object. (self.myName!=this.myName)

Scope traversal means, when you are reaching to grab a value (variable,object) that exists in a different scope, therefore additional overhead is added (code becomes slower to execute).

Using bind, you 're calling a function with an existing scope, so that scope traversal does not take place.

4 Comments

code becomes slower to execute - this is completely wrong as indicated by sbr's comment and other SO questions: stackoverflow.com/questions/17638305/…
Can you provide more context surrounding the line of code you added? The link provided in the original question is no longer valid.
Also, can you provide any documentation or evidence to explain "when you are reaching to grab a value that exists in a different scope, additional overhead is added". The performance check linked above by @sbr seems to show that closure is the same - even a bit faster.
Not so: self is generally faster than bind in 2015: jsperf.com/bind-vs-closure-setup/6
0

jsperf is dead. meet perf.link.

i'm seeing a different victor each time i run the benchmark. my guess is, modern browsers optimize away most of the disadvantages of either of these approaches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.