Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Usage with Optional Chaining #101

Open
boopathi opened this issue Jul 29, 2017 · 3 comments
Open

Usage with Optional Chaining #101

boopathi opened this issue Jul 29, 2017 · 3 comments

Comments

@boopathi
Copy link
Contributor

boopathi commented Jul 29, 2017

Call semantics and Usage with optional chaining proposal

  1. Is it possible to combine optional chaining operator and shorthand access for private property ? - I assume the answer is No and use this?.#x?
  2. Related to Call semantics #85 : in the below defined method foo, #x and this.#x should refer to the same private field of instance p.
class A {
  #x;
  constructor(x, a) {
    #x = x;
    this.foo = this.foo.bind(a);
  }
  foo() {
    console.log(this?.#x); // optional chaining - logs undefined for `p.foo()`

    // Possible to use optional chaining for #x ?

    // And does this.#x and #x refer to same thing?
    console.log(#x, this.#x); // both should throw for `p.foo()`
  }
}

const p = new A(10);
const q = new A(20, p);

p.foo(); // should throw here?
q.foo();
@ljharb
Copy link
Member

ljharb commented Jul 29, 2017

Absent the chaining operator, of course p.foo() should throw because it's bound to undefined, and not an instance of A.

The #x shorthand has been deferred to a separate proposal, but yes, I believe #x and this.#x would always mean the same thing.

@bakkot
Copy link
Contributor

bakkot commented Jul 30, 2017

See #78.

We haven't worked out the interaction yet, though. As this proposal is more advanced than that one, I expect it will be worked out as part of advancing it, rather than here.

@littledan
Copy link
Member

@boopathi To answer the question, I think we could add a x?.#y operator (as described in #78), but the optional chaining proposal is evolving towards including only useful operations, and it's a bit hard for me to believe that this is a useful combination.

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

No branches or pull requests

4 participants