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

Support optional private prop? #148

Open
fisker opened this issue Oct 29, 2020 · 10 comments
Open

Support optional private prop? #148

fisker opened this issue Oct 29, 2020 · 10 comments

Comments

@fisker
Copy link
Collaborator

fisker commented Oct 29, 2020

class A{a(){obj?.#x.a}}
@3cp
Copy link
Member

3cp commented Oct 29, 2020

How did you access a private prop from outside of class definition of obj?

> class B {
... #p = 2;
... foo() { return this.#p; }
... }
undefined
> var b = new B;
undefined
> b.foo()
2
> b.#p
... ;
b.#p
 ^

Uncaught SyntaxError: Private field '#p' must be declared in an enclosing class
> b['#p']
undefined

@fisker
Copy link
Collaborator Author

fisker commented Oct 29, 2020

class A{
	#x = {a: 1}

	a(){
		return this?.#x.a
	}
}

new A().a()

@fisker
Copy link
Collaborator Author

fisker commented Oct 29, 2020

class A{
	#x = {a: 1}

	a(){
        const obj = this
        return obj?.#x.a
	}
}

new A().a()

@fisker
Copy link
Collaborator Author

fisker commented Oct 29, 2020

How did you access a private prop from outside of class definition of obj?

Not outside, inside.

@3cp
Copy link
Member

3cp commented Oct 29, 2020

obj?.#x means obj ? obj.#x : undefined. So it doesn't make much sense here because this is never undefined.

It probably makes sense for obj.#y?.a, but again it does not really make sense, because private prop is statically checked by js compiler.

> class A{
...   #x = {a: 1}
... 
...   a(){
...         const obj = this
...         return obj.#y.a
...   }
... }
... ;
        return obj.#y.a
                  ^

Uncaught SyntaxError: Private field '#y' must be declared in an enclosing class

One possible usage might be

class A {
  #x; // undefined
  a() {
    return this.#x?.a;
  }
}

And yes, this code is fine in meriyah.

My argument is because private prop is statically checked, it doesn't make any sense to have any?.#private syntax. I will be surprised if ecma allows this. @KFlash any thought?

@KFlash
Copy link
Contributor

KFlash commented Oct 29, 2020

Just follow what the proposal says

@fisker
Copy link
Collaborator Author

fisker commented Oct 29, 2020

You point make sense, I wounder why Private Fields in in? Seems static check fails on superClass privete field too

class A{
	#x = {a: 1}
}
class B extends A{
	a(){
        return this?.#x.a
	}
}

new B().a()
Uncaught SyntaxError: Private field '#x' must be declared in an enclosing class

@fisker
Copy link
Collaborator Author

fisker commented Oct 29, 2020

This?

class A {
#a=1
a() {return this?.#a}
}

A.prototype.a.call(undefined)
A.prototype.a.call(new A)

@3cp
Copy link
Member

3cp commented Oct 29, 2020

Private class fields and methods

Issue #28: Should optional chains support the upcoming private class fields and private methods, as in a?.#b, a?.#b() or a?.b.#c? Quoting microsoft/TypeScript#30167 (comment):

This one isn't baked into the proposal yet, simply because private fields themselves aren't baked yet. So we don't want to hold up this proposal if that one happens to stall out. Once that one has reached Stage 4, we will address it then.

tc39/proposal-optional-chaining#28
tc39/proposal-class-fields#301
tc39/proposal-class-fields#302

@3cp
Copy link
Member

3cp commented Oct 29, 2020

In short, there were lots of arguments like ours. But it's in the proposal now.

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

3 participants