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 functions private #1537

Closed
shmuelie opened this issue Dec 19, 2014 · 5 comments
Closed

Make private functions private #1537

shmuelie opened this issue Dec 19, 2014 · 5 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@shmuelie
Copy link

Currently private functions are put on the prototype of a function so they are not really private. I suggest adding them as normal functions in the scope of the class so that they are in fact only accessible to the class.

Example

module Example
{
    export class ExampleClass
    {
        method()
        {
            this.privateMethod();
        }
        private privateMethod()
        {
            console.log("private");
        }
    }
}

would create

var Example;
(function (Example) {
    var ExampleClass = (function () {
        function ExampleClass() {
        }
        function ExampleClass_privateMethod()
        {
            console.log("private");
        }
        ExampleClass.prototype.method = function () {
            privateMethod();
        };
        return ExampleClass;
    })();
    Example.ExampleClass = ExampleClass;
})(Example || (Example = {}));
@DanielRosenwasser
Copy link
Member

We've had this discusion before and I don't think we'd be willing to take this approach given the original reasons as well as the fact that it's not really backwards compatible anymore.

@DanielRosenwasser DanielRosenwasser added the Suggestion An idea for TypeScript label Dec 19, 2014
@shmuelie
Copy link
Author

Ah, should have checked the Codeplex site too for this. I do see the issues but not really the backward compatibility issue. Since private methods should never be called outside the class only code that would break is code that should break.

@DanielRosenwasser
Copy link
Member

Unfortunately, "should never" is not the same as "has never" - users may have previously used privates to emulate other levels of privacy like C++'s friend that are not in the language - basically along the same lines of "I know that these members are here but nobody else should."

@RyanCavanaugh RyanCavanaugh added By Design Deprecated - use "Working as Intended" or "Design Limitation" instead and removed Suggestion An idea for TypeScript labels Dec 19, 2014
@RyanCavanaugh
Copy link
Member

In addition to the issues in the linked thread, note that the this reference would be wrong in the private method in the proposed codegen.

@shmuelie
Copy link
Author

Fair enough

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

3 participants