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

Add private class fields #8623

Closed
uMaxmaxmaximus opened this issue May 16, 2016 · 2 comments
Closed

Add private class fields #8623

uMaxmaxmaximus opened this issue May 16, 2016 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@uMaxmaxmaximus
Copy link

uMaxmaxmaximus commented May 16, 2016

You private props is not private:

  1. The code is not written on the typescript (which accordingly is not subject to static analysis) can access them.

  2. there is a naming conflict in the child classes (child classes can not use the same private properties of the parent class name, even though the conflict of names should not be)

image

SOLUTION:

compile this code

class Animal {
    private handlers = []
}

class Rebbit extends Animal {
    private handlers = []
}

to ES6:

class Animal {
    let _private_handlers = Symbol('Animal.handlers')

    constructor(){
        this[_private_handlers] = []
    }
}

class Rebbit extends Animal {
    let _private_handlers = Symbol('Rebbit.handlers')

   constructor(){
        this[_private_handlers] = []
    }
}

Simplified Symbol es5 pollyfill:

function Symbol(name){ return name + Math.random()  }

Yes, this is not in the semantics of the javascript, but then your private property modificator too is not in the semantics of the javascript, but you've added them as a convenience.

protected props

class Animal {
    protected handlers = []
}

class Rebbit extends Animal {
    protected handlers = []
}

compile to ES6:

class Animal {
    constructor(){
       this._handlers = []
    }
}

class Rebbit extends Animal {
    constructor(){
       this._handlers = []
    }
}

the only way it will not work, expressions like this[propName] because we need to know the name of the property to which the access, in advance at compile time, so if it is a private or protected, then replace it with the proper structure: private to this[symbol], protected to this._prop

@DanielRosenwasser DanielRosenwasser added the Design Limitation Constraints of the existing architecture prevent this from being fixed label May 16, 2016
@DanielRosenwasser
Copy link
Member

Hey @uMaxmaxmaximus

  1. That ES6 code isn't valid - you can't declare a let inside of a class. Perhaps you meant something like this entry in our FAQ.
  2. We've looked into using Symbols for privates in Implement private variables, getter/setters and methods using Symbols #685 but it didn't turn out well.

In general this comes up every once in a while, but treating private members as members on the object itself seems to still be the best approach to use.

@uMaxmaxmaximus
Copy link
Author

ok, i write coffeescript mod to (typescript and es6) and impliment private props, since you can not

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants