top | item 944211

Learning Advanced JavaScript

52 points| bensummers | 16 years ago |ejohn.org | reply

15 comments

order
[+] bumblebird|16 years ago|reply
Slightly off-topic, but:

>> #2: Goal: To be able to understand this function:

idk, I find all the js library bind() functions to be a bit yucky. Closures aren't hard:

onclick = function(c){return function() { c.doSomething(stuff,theOtherStuff);}}(this);

Also, they're far more general when you write them like that, and you're not asking the runtime to constantly call array methods etc

Useful resource though, but don't use a js lib :)

[+] jacobolus|16 years ago|reply
They’re much less readable when you write them like that, because when you get a few dozen lines down, this is much clearer than c, especially for people trying to read your code who are used to writing idiomatic JavaScript.

Also, I can live with defining such an argument on initial definition, as with Python’s self in method definitions, but having to add an extra argument to every function/method invocation for the execution scope, that must be passed in every time explicitly or everything breaks, is really obnoxious. You could certainly make up a convention for the proper name, but just using this is more straight-forward, and doesn’t really have any particularly significant downsides.

There’s a reason that pretty much every JavaScript framework includes a bind function/method, and that they’re adding it to the official language spec: it’s a convenient and effective pattern.

[+] mrkurt|16 years ago|reply
Certain browsers leak memory like mad when you use closures. The libs bind functions usually account for that and do the necessary cleanup.

Plus, the jQuery "bind" function is necessary so you can keep chaining methods.

[+] diN0bot|16 years ago|reply
ok, but once you start using closures everywhere someone has the bright idea to abstract most of that code into a utility function. before you know it, you're back to bind.

  function _bind(o, m) {
    var bound_arguments = Array.prototype.slice.apply(arguments, [2]);
    return function() {
      var now_arguments = Array.prototype.slice.apply(arguments);
      var args = bound_arguments.slice().concat(now_arguments);
      return m.apply(o, args);
    }
  }
[+] hendrik|16 years ago|reply
I really like his approach to teaching JS, but I wish he would finally finish his "Secrets of the JavaScript Ninja" book. If I recall correct, I pre-ordered this book almost 2 years ago, when it had a release date of a few months later.