A LIST Apart: For People Who Make Websites

No. 262

Discuss: Getting Out of Binding Situations in JavaScript

Pages

 1 2 3 >  Last »

1 Untitled

Binding is never required but could save certain issues for sure.

posted at 04:46 am on July 2, 2008 by a gatt

2 Untitled

This was a very informative article – I was previously unaware of apply and call in javascript, and had been using lexical closures to preserve binding where I needed it – and in some cases, this was quite clunky.

Thanks for the info!

posted at 07:12 am on July 2, 2008 by Jeremiah Dodds

3 YUI Library

The excellent YUI library does this as well, both in event binding, and for instance YAHOO.lang.later (too preserve scope in window.setTimeout/Interval).

posted at 08:26 am on July 2, 2008 by Einar Qvale

4 what about with?

[code]
name = ‘Ray’; // Or explictly: window.name = ‘Ray’;
var john = { name: ‘John’, greet: function(person) { with(this) alert(“Hi “ + person + “, my name is “ + name); }
};

john.greet(“Mark”);
// => “Hi Mark, my name is John”
[/code]

posted at 10:33 am on July 2, 2008 by Andrea Giammarchi

5 Helpful, thank you

I’m the “var that = this” kind of person (actually, var scope = this).

Scope, and object oriented programming in Javascript in general, has cost me many headaches. This articles gives extra insight and explains binding in a very clear way. Thank you Christophe!

posted at 12:48 pm on July 2, 2008 by Blaise Kal

6 Excellent summary

Thank you, it’s very helpful to have related programming tricks brought together like this. In JavaScript you need them to do practically anything more advanced with objects, yet you won’t find it in any language reference — because it’s a usage pattern, not part of the syntax…

I’m pretty new to JavaScript, and I have stumbled upon this problem several times already. Solved it with “var that=this” following Douglas Crowford, but creating a helper function might have been much more convenient in many cases.

Thank you.

posted at 01:53 pm on July 2, 2008 by Radomir Dopieralski

7 Excellent article

One of the best technical articles I’ve read in a year. The clarity, level of detail, and the flow from low level to high level is just brilliant.

Thank you. I wish all technical material was in this tone and level.

posted at 02:59 pm on July 2, 2008 by Mina Naguib

8 Again with the Rails vs PHP

I have read every alistapart article, and I absolutely love this site, but I still have to say, I’m so sick of everyone from the train-yard bagging PHP. I feel so much animosity from RoR as if there was a war between two of the most promising open source programming solutions. Every time an article is written about OOP PHP is bagged. PHP is much (5 times) faster than Ruby, which is one reason why development of PHP, and new users are growing at such an alarming rate.

posted at 03:41 pm on July 2, 2008 by Mike Mike

9 Re: Again with the Rails vs PHP

Huh, dude? How the hell did you manage to draw that conclusion from an article focusing on Javascript?

posted at 04:32 pm on July 2, 2008 by Srdjan Pejic

10 Woah, thanks guys!

Einar> Yes, YUI does it too, sorry I failed to mention it. D’oh, Christian will kill me next time I see him!

Andrea> Essentially, with is evil. Seriously. Just about every serious expert will advise against with, regardless of the programming language (e.g. JavaScript or Delphi). Essentially, it tampers too much with scope and binding rules, but also leads to misunderstanding the code in languages with implicit binding (it’s all too easy to skip over the with call while reading the code inside). Also, in the context of binding, with won’t help much: passing a method reference will lose its binding anyhow. Consider the following:

name = ‘Christophe’;
var andrea = {
  name: ‘Andrea’,
  introduce: function() { alert(“Hi I’m “ + this.name + ‘!’); }
};
function times(count, fx) {
  for (var index = 0; index < count; ++index)
    fx();
};
with (andrea)
  times(1, introduce);

This will still alert “Hi, I’m Christophe!”. So you with only introduces confusion…

Blaise, Radomir> Thanks, guys! :-)

Mina> You just made my day :-)

Mike> Er, I can understand your frustration (we Railsers do tend to bash on PHP more than is necessary, or even fair), but where the heck did I even mention Rails in my article? PHP’s great for specific purposes (e.g. very quickly whipping and deploying a simple online feature), but yes, its OOP support is lackluster, I’m sorry, it’s just true. Although the situation vastly improved with 5.x, it still doesn’t come close to any other prominent OO language, be it Java, C#, C++, Objective C… Also, I personally believe your speed argument is flawed: speed is not why new people flock to PHP all the time: they do because its learning curve is next to flat, and it’s supported by every single web host out there.

Anyway, thanks so much for the feedback everyone!

posted at 04:44 pm on July 2, 2008 by Christophe Porteneuve

Pages

 1 2 3 >  Last »

Got something to say?

Discuss this article. We reserve the right to delete flames, trolls, and wood nymphs.

Create a new account or sign in below if you’d like to leave a comment.

Remember me

Forgot your password?

Subscribe to this article's comments: RSS (what’s this?)