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

make private static properties real private? #8299

Closed
wyntau opened this issue Apr 26, 2016 · 6 comments
Closed

make private static properties real private? #8299

wyntau opened this issue Apr 26, 2016 · 6 comments
Labels
Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design

Comments

@wyntau
Copy link

wyntau commented Apr 26, 2016

TypeScript Version:

1.8.7

Code

class A {
  private static names: string[] = [];

  public pushNames(name: string): void {
    A.names.push(name);
  }
}

Expected behavior:
have real private static properties names

var A = (function () {
    var names = [];
    function A() {
    }
    A.prototype.pushNames = function (name) {
        names.push(name);
    };
    return A;
}());

Actual behavior:
does not have real private static properties. A.names is not private when compiled to javascript

var A = (function () {
    function A() {
    }
    A.prototype.pushNames = function (name) {
        A.names.push(name);
    };
    A.names = [];
    return A;
}());

We can only implement this for private static properties, and leave private instance properties as what it is now or add prefix underscore _ or something else for private instance properties?

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Apr 26, 2016

Hey @treri, in the future we have an FAQ that you can check out before filing suggestions/issues. The relevant suggestion is You should emit classes like this so they have real private members. As explained in the FAQ, your solution won't actually function correctly because it will create a single variable shared among all instances.

See also #684.

@DanielRosenwasser DanielRosenwasser added the Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design label Apr 26, 2016
@wyntau
Copy link
Author

wyntau commented Apr 26, 2016

@DanielRosenwasser My meaning is private static properties not private properties(instance properties). Maybe you didn't see my last note.

My aim is to create a variable belong to the Class, not the instance

@wyntau
Copy link
Author

wyntau commented Apr 26, 2016

@DanielRosenwasser Could you please give me a expansion for my suggestion for only static properties?

@wyntau wyntau changed the title Support real private static properties? make private static properties real private? Apr 26, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2016

I do not see why instance and static should behave differently. Please note that you can capture the variables if you want inside a closure and/or a namespace declaration

@sjohnsonaz
Copy link

sjohnsonaz commented Jun 12, 2016

@DanielRosenwasser, the FAQ explains why private variables should not be done outside the constructor, but inside the self executing function. However, that is a valid implementation of private static variables, which is what I believe @treri is looking for. I'm running into this problem in my own project, as I convert my classes from pure JavaScript into TypeScript.

@juanmendes
Copy link

@mhegazy They do behave differently already, one adds a property to the constructor, the other adds a property to the object.

@DanielRosenwasser We're just saying that if it doesn't need to be access by the outside, why pollute the constructor object and get the benefit of being truly private, even in JS?

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design
Projects
None yet
Development

No branches or pull requests

5 participants