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

[Example] const is equal to multiple numbers #286

Open
AlexXanderGrib opened this issue Oct 10, 2022 · 0 comments
Open

[Example] const is equal to multiple numbers #286

AlexXanderGrib opened this issue Oct 10, 2022 · 0 comments
Labels
new-example A proposal of the new example

Comments

@AlexXanderGrib
Copy link

Example

const a = new Proxy({ i: 1 }, { 
  get: (target) => () => target.i++,
})

console.log(a == 1, a == 2, a == 3);

Explaination

  1. Every time when comparing, property [Symbol.toPrimitive] is accessed
  2. Proxy returns a function, that is called
  3. So value is compared with result of the function
const a = new Proxy({ i: 1 }, {
  get(target, property) {
    console.trace("[Proxy] get() called with", { target, property });

    return function inner() {
      console.trace("[Proxy] inner() called");

      return target.i++;
    }
  }
})

console.log(a == 1, a == 2, a == 3);

Log

[Proxy] get() called with
{
  target: { i: 1 },
  property: Symbol(Symbol.toPrimitve)
}
[Proxy] inner() called
...

Basically, object is equal to

const a = {
  i: 1,
  [Symbol.toPrimitive]() {
    return this.i++;
  }
}
@AlexXanderGrib AlexXanderGrib added the new-example A proposal of the new example label Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-example A proposal of the new example
Projects
None yet
Development

No branches or pull requests

1 participant