Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object methods not supported #18

Open
robbiespeed opened this issue Aug 17, 2024 · 1 comment
Open

Object methods not supported #18

robbiespeed opened this issue Aug 17, 2024 · 1 comment

Comments

@robbiespeed
Copy link

Methods will loose their intended this value due to Function's Symbol.result wrapper which will override the this value to the method itself.

Ex:

const o = {
  r: 10,
  e: -10,
  method (v) {
    if (v > 0) {
     return this.r;
    } else {
      throw this.e;
    }
  }
};

o.method.r = 1000;

const [error, result] ?= o.method(2); // [null, 1000]

As you can see the this.r ends up retrieving it's value from o.method instead of o itself.

In it's current form Symbol.result can't preserve this, and it would also be very strange if it was a special case method on functions.

One solution is to change the signature of Function.prototype[Symbol.result] methods to match that of Function.prototype.call which is thisArg, ...arguments. So the code from the example would be equivalent to:

const [error, result] = o.method[Symbol.result].call(o, 2); // [null, 10]
@anacierdem
Copy link

Also related to #12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants