10 biggest JS pitfalls

Axel Rauschmayer axel at rauschma.de
Sun Dec 30 15:26:18 PST 2012

> You don't mean for-of, you mean for (let i...) in any form. We want all for-let forms to make fresh let bindings per iteration. That's what your resolution for (7) should say, and ES6 does indeed aim to do this.

Yes, thanks. I thought only for-of did this.

>>> We can work around it by changing the constructor:
>>> function A() {
>>>  this.a = 123;
>>>  this.p = this.p.bind(this);
>>> }
>>> but of course now we are over-allocating for no good reason IMO.  Very frustrating.
>> Yes, that’s (9c). I would love for JS to make the distinction between reading a property and invoking a function.
> If you "fix" this by making extraction of a function value from a property auto-bind, which will have the same overhead under the hood that Wes cites in his explicit bind-based version.
> If you let the function be extracted with unbound |this|, then you'll have bugs where it is called on the wrong |this| (undefined, let's hope).
> You can't have it both ways. In particular, auto-bind on extract costs (even moreso if there's a memo under the hood to reuse the same single bound method -- which is mutable so would make a side channel).
> JS is functional first, not OOP first. APIs that want methods to be extractable as bound to the object from which they were extracted must do extra work, which can be self-hosted via getters for specific methods, or a general proxy.

Again, not sure if that’s feasible, but it would help if one could make the distinction between a property being read (“get”) and a method being invoked (“call”).

function extractableMethod(...) { ... }
Object.defineProperty(Foo.prototype, 'extractableMethod', {
    get() {
        return extractableMethod.bind(Foo.prototype);
    call(...args) {
        return extractableMethod.call(Foo.prototype, ...args);

Still messy and probably not backwards compatible, so a bind operator might be a better idea, after all:
    elem.onclick = obj::foo;  // or the proposed ::obj.foo
instead of
    elem.onclick = obj.foo.bind(obj);

Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121231/6b7b49cf/attachment-0001.html>

More information about the es-discuss mailing list