TypeScript 中的私有“函数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16919473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:05:10  来源:igfitidea点击:

Private "functions" in TypeScript

typescript

提问by Richard

Is it possible to create a private "function" (method) within a TypeScript class? Let's assume we have the following Person.tsTypeScript file:

是否可以在 TypeScript 类中创建私有“函数”(方法)?假设我们有以下Person.tsTypeScript 文件:

class Person {
    constructor(public firstName: string, public lastName: string) {
    }

    public shout(phrase: string) {
        alert(phrase);
    }

    private whisper(phrase: string) {
        console.log(phrase);
    }
}

Which when compiled is being transformed to the following:

编译时将转换为以下内容:

var Person = (function () {
    function Person(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    Person.prototype.shout = function (phrase) {
        alert(phrase);
    };
    Person.prototype.whisper = function (phrase) {
        console.log(phrase);
    };
    return Person;
})();

Observations

观察

I was expecting the whisperfunction to be declared within the closure, but not on the prototype? Essentially this makes the whisperfunction public when compiled?

我期望whisper在闭包中声明函数,而不是在原型上声明?基本上这会使whisper函数在编译时公开?

回答by Jude Fisher

TypeScript public/private keywords only apply to the way TypeScript checks your code - they don't have any effect on the JavaScript output.

TypeScript public/private 关键字仅适用于 TypeScript 检查代码的方式 - 它们对 JavaScript 输出没有任何影响。

According to the language specification(pp. 9-10):

根据语言规范(第 9-10 页):

Private visibility is a design-time construct; it is enforced during static type checking but does not imply any runtime enforcement. ... TypeScript enforces encapsulation of implementation in classes at design time (by restricting use of private members), but cannot enforce encapsulation at runtime because all object properties are accessible at runtime. Future versions of JavaScript may provide private names which would enable runtime enforcement of private members

私有可见性是一种设计时构造;它在静态类型检查期间强制执行,但并不意味着任何运行时强制执行。... TypeScript 在设计时强制封装类中的实现(通过限制私有成员的使用),但不能在运行时强制封装,因为所有对象属性都可以在运行时访问。JavaScript 的未来版本可能会提供私有名称,这将启用私有成员的运行时强制执行

This has already been asked and answered here: https://stackoverflow.com/a/12713869/1014822

这已经在这里提出并回答:https: //stackoverflow.com/a/12713869/1014822

Update:This old answer still gets an amount of traffic, so worth noting that, in addition to the language spec link above, public, private, and (now) protected members are covered in detail in the TypeScript handbookchapter on classes.

更新:这个旧的答案仍然获得了大量的流量,所以值得注意的是,除了上面的语言规范链接之外,公共、私有和(现在)受保护的成员在 TypeScript手册中关于类的章节中有详细介绍。

2018 UpdateImplementation of ES Private Fields is now a Future item on the TypeScript RoadMapalthough discussionsuggests this will be a parallel hard private option, not a replacement for the current soft private implementation.

ES 私有字段的2018 更新实现现在是 TypeScript路线图上的未来项目,尽管讨论表明这将是一个并行的硬私有选项,而不是当前软私有实现的替代品。

回答by SLaks

In Javascript (as opposed to TypeScript), you can't have a private "member" function.

在 Javascript(与 TypeScript 相对)中,您不能拥有私有的“成员”函数。

If you define a private function in the closure, you won't be able to call it as an instance method on an instance of your class.

如果您在闭包中定义了一个私有函数,您将无法将其作为类实例的实例方法调用。

If that's what you want, just move the TypeScript function definition outside the class body.

如果这就是您想要的,只需将 TypeScript 函数定义移到类主体之外。

回答by air5

You can use private class variables and functions in TypeScript/Webpack and even in the latest Google Chrome versions natively. Just add a #before the name. See here.

您可以在 TypeScript/Webpack 甚至最新的 Google Chrome 版本中使用私有类变量和函数。只需#在名称前添加一个。见这里

MyClass{

  #privateMember: Number = 4 

  #privateMethod() {
      this.#privateMember = 3
  }
}