>   4. Control of execution (that is, the actuall calling of a function) is
> linear in the chain in the calling code, rather than being controlled by the
> code inside a function.
>

Another reason I feel it's important to address being able to retain control
of the execution of the chained functions in *my* calling code is the
awkwardness around function context objects and them losing their `this`
binding.

We know that this:

function foo(cb) {
   setTimeout(cb,1000);
}
var obj = {
   val: 1,
   bar: function() {
      this.val = (this.val || 0) + 1;
      console.log(this.val);
   }
};
foo(obj.bar);

...is subject to the `this` binding being lost from "obj" and becoming
"window". So, we have to go through hoops to "bind" a function's `this`
tightly, with Function.prototype.bind(), or we create a manual anonymous
function wrapper, etc. While it's possible to correct for, obviously, it
creates extra work for the developer and is a common trap for subtle errors.

Instead:

foo() @ obj.bar();

Since my calling code is in control of the execution of `bar`, I call it on
the proper object and `this` doesn't get lost.


--Kyle
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to