Skip to content

class private methods shouldn't be "exported" #6748

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

Closed
OneOfOne opened this issue Jan 30, 2016 · 3 comments
Closed

class private methods shouldn't be "exported" #6748

OneOfOne opened this issue Jan 30, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@OneOfOne
Copy link

Pretty much like the title says, private methods should stay private:

input:

class NoRespect {
    private priv() {}
    pub() { return this.priv(); }
} 

actual output:

var NoRespect = (function () {
    function NoRespect() {}
    NoRespect.prototype.priv = function () { };
    NoRespect.prototype.pub = function () { return this.priv(); };
    return NoRespect;
})();

expected output:

var NoRespect = (function () {
    function NoRespect() {}
    var priv = function () { };
    NoRespect.prototype.pub = function () { priv.call(this); };
    return NoRespect;
})();
➜ tsc --version
message TS6029: Version 1.7.5
@yortus
Copy link
Contributor

yortus commented Jan 30, 2016

Duplicate of #1537?

@DanielRosenwasser DanielRosenwasser added Duplicate An existing issue was already created Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design labels Jan 30, 2016
@DanielRosenwasser
Copy link
Member

Thanks @yortus!

@RyanCavanaugh RyanCavanaugh removed the Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design label Feb 1, 2016
@sant123
Copy link

sant123 commented Jun 23, 2016

@yortus Either way is duplicated or not, the solution of private methods would be fine. As you can see, it really hiddes the method but it continues using the this keyword. Besides, private methods should stay as the name says 'only for the class' and Typescript compilation supports it! But that method can be consumed by normal Javascript because is inside the prototype of the class.

Please take a look at this example:

(function() {
    var Person = (function() {
        function secretMessage() {
            return "Hi " + this.name + " " + this.lastName;
        }

        function Person(name, lastName) {
            this.name = name;
            this.lastName = lastName;
        }

        Person.prototype.greet = function() {
            return secretMessage.call(this);
        };

        return Person;
    }());

    var c = new Person("Santiago", "Aguilar");
    console.log(c.greet()); //will greet :)
    console.log(c.secretMessage()); //ey ey this function is private, and can't be reached! Because it's private!
})();

@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

5 participants