andreypopp / autobind-decorator

Decorator to automatically bind methods to class instances
MIT License
1.45k stars 66 forks source link

Make it work without requiring Symbol polyfill #2

Closed andreypopp closed 9 years ago

andreypopp commented 9 years ago

The best would be to have babel to transpile Symbol itself.

gaearon commented 9 years ago

Same for Reflect

chicoxyzzy commented 9 years ago

so babel will add local polyfill but target browsers could be Symbol-ready. so we loose in performance and module size in that case

sebmck commented 9 years ago

Why use the symbol at all?

Just change:

return {
  configurable: true, // must be true or we could not be changing it
  get() {
    if (!this.hasOwnProperty(_key)) {
      this[_key] = fn.bind(this);
    }
    return this[_key];
  }
};

to

return {
  configurable: true, // must be true or we could not be changing it
  get() {
    var fn = this[key].bind(this);
    Object.defineProperty(this, key, {
      configurable: true,
      writable: true,
      value: fn
    });
    return fn;
  }
};

The current way is slower too since the overhead of the getter is going to be there every single time, regardless of your use of the private bound method.

andreypopp commented 9 years ago

@sebmck you are right, that's actually how it was implemented initially https://github.com/andreypopp/autobind-decorator/commit/fb29744229f06bec8575e089f7c5fe28a407c6e5#diff-1fdf421c05c1140f6d71444ea2b27638