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

private Members #8847

Closed
Mephiztopheles opened this issue May 26, 2016 · 11 comments
Closed

private Members #8847

Mephiztopheles opened this issue May 26, 2016 · 11 comments
Labels
Duplicate An existing issue was already created

Comments

@Mephiztopheles
Copy link

Mephiztopheles commented May 26, 2016

HI, i wanted to look at typescript and saw, that private members are not really private in console :D
i searched in your FAQ and found an entry to this, but i think this might be a solution for private members.

TypeScript Version:

1.8.9

Code

// you all know it:
class Foo {
  private x = 0;
  increment(): number {
      this.x++;
      return x;
  }
}

Wished behavior:

// what about something like this?
var Foo = (function () {
    var privates = {};

    function Foo() {
        privates[ this ] = { x: 0 };
    }

    Foo.prototype.increment = function () {
        privates[ this ].x++;
        return privates[ this ].x;
    };
    return Foo;
})();

Actual behavior:

// generates
var Foo = (function () {
    var x = 0;

    function Foo() {
    }
    Foo.prototype.increment = function () {
        x++;
        return x;
    };
    return Foo;
})();
@mhegazy
Copy link
Contributor

mhegazy commented May 26, 2016

@mhegazy mhegazy closed this as completed May 26, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label May 26, 2016
@Mephiztopheles
Copy link
Author

I gave a different solution than your example why is this duplicate?
Did you even read my post? I eon'the think so
Say something more than duplicate instead of just closing it!

@RyanCavanaugh
Copy link
Member

Try running your proposed code with two instances of Foo and see what happens. It doesn't work.

Suffice to say if there were a good simple solution here, we would have done it. There are no small clever tricks that generate private members without seriously bad trade-offs.

@Mephiztopheles
Copy link
Author

Mephiztopheles commented May 26, 2016

But I tried... I would never post a solution if I wouldn't test it before... due to var a = {}, b = {a:a}; a === b.a will return true , the privates object will creates privates for each instance of foo. In case you use the new Operator.

@RyanCavanaugh
Copy link
Member

image

@Mephiztopheles
Copy link
Author

ya, seems to be in my check i typed last line x instead y :D
and how a bout this? I just want to help :)

var Foo = (function () {
    var privates = [];
var instances = [];
    function Foo() {
        instances.push(this);
        privates.push( { x: 0 } );
    }

    Foo.prototype.increment = function () {
        privates[ instances.indexOf(this) ].x++;
        return privates[ instances.indexOf(this) ].x;
    };
    return Foo;
})();

@RyanCavanaugh
Copy link
Member

Now doing something with a list of Foos is an O(n^2) operation instead of an O(n) operation. That's not a good trade-off!

@Mephiztopheles
Copy link
Author

i don't get it... its too slow? or what's the matter?

@RyanCavanaugh
Copy link
Member

O(n^2) complexity to walk a list of n elements and access their private members is too slow, yes.

@Mephiztopheles
Copy link
Author

hm... seems to be that i am not programming in high performance :D
and whot does O(n^2) mean? Object with n*n properties?
i never used private members before ... so never in that case... i just saw the docs and tested and came to that point but if it's too slow (especially when you created a lot of Foo) then never mind

@DanielRosenwasser
Copy link
Member

Hey @Mephiztopheles, it's called Big O Notation. Check out a simple explanation or a more in-depth one on Wikipedia for more details.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants