typescript 理解打字稿类中的“公共”/“私有”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38713052/
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
Understanding "public" / "private" in typescript class
提问by refactor
In the below type script code , irrespective of whether name is "public" or "private" , java script code that is generated is same.
在下面的类型脚本代码中,无论名称是“public”还是“private”,生成的java脚本代码都是一样的。
So my question is, how to decide when the constructor parameter should be public or private ?
所以我的问题是,如何决定构造函数参数何时应该是 public 或 private ?
// typescript code
class Animal {
constructor( public name: string) {
}
}
// generated JS code
var Animal = (function () {
function Animal(name) {
this.name = name;
}
return Animal;
}());
采纳答案by basarat
java script code that is generated is same
生成的java脚本代码相同
They produce the same JavaScript but don't have the same semantics as far as the type is concerned.
它们生成相同的 JavaScript,但就类型而言没有相同的语义。
The private
member can only be accessed from inside the class whereas public
can be excessed externally.
该private
成员只能从类内部访问,而public
可以从外部访问。
More
更多的
The differences are covered here : https://basarat.gitbooks.io/typescript/content/docs/classes.html#access-modifiers
此处涵盖了差异:https: //basarat.gitbooks.io/typescript/content/docs/classes.html#access-modifiers
Another example
另一个例子
let foo = 123;
will generate the same ES5 as
将生成与相同的 ES5
const foo = 123;
However in the first case let foo = 123;foo = 456
will compile fine but const foo = 123; foo = 456
will result in a compile time error.
然而,在第一种情况下let foo = 123;foo = 456
会编译正常,但const foo = 123; foo = 456
会导致编译时错误。
回答by Josiah Nunemaker
The public, private, protected access modifiers, as you have discovered, don't actually affect the final outputted code. What they doaffect is the type checking at compile time.
正如您所发现的,public、private、protected 访问修饰符实际上并不影响最终输出的代码。他们不影响的类型是在编译时检查。
What do they actually do?
他们实际上是做什么的?
As their names suggest, the public
and private
modifiers limit what can access the class member. Their is also a third modifier in the clan, protected
.
顾名思义,public
和private
修饰符限制了可以访问类成员的内容。他们也是氏族中的第三个修饰符,protected
。
The privatemodifier only allows a class member (variable or method) to be accessed within that class.
的私人改性剂只允许一个类的成员(变量或方法)将被访问该类别内。
The protectedmodifier allows everything the private
modifier does, and also allows other classes that extend that class to use it.
该保护的修饰符允许的一切private
修改器,并且还允许继承了该类其他类使用它。
Finally, the publicmodifier makes it so anything can access the class also has access to the public class property.
最后,public修饰符使得任何可以访问该类的东西也可以访问 public 类属性。
For a more in-depth explanation and examples, take a look at the official TypeScript Handbook's explanation.
对于更深入的解释和示例,请查看官方 TypeScript 手册的解释。
If it all compiles the same, why should I use the modifiers?!
如果全部编译相同,我为什么要使用修饰符?!
Using the modifiers will enable the compiler to make sure that your code isn't using things that it shouldn't be using. This is the same reasoning behind using types in the first place, it makes it harder to make mistakes that shouldn't be able to be made in the first place! As an added bonus, if your text editor has TypeScript support, it will also use the access modifiers when showing you autocomplete values for variables and methods.
使用修饰符将使编译器能够确保您的代码没有使用它不应该使用的东西。这与首先使用类型背后的原因是相同的,它使犯一开始不应该犯的错误变得更加困难!作为一个额外的好处,如果您的文本编辑器支持 TypeScript,它还会在显示变量和方法的自动完成值时使用访问修饰符。
回答by kdaShivantha
In ESnext, private class fieldsare defined using a hash # prefix:
在 ESnext 中,私有类字段使用哈希 # 前缀定义:
class MyClass {
a = 1; // .a is public
#b = 2; // .#b is private
static #c = 3; // .#c is private and static
incB() {
this.#b++;
}
}
const m = new MyClass();
m.incB(); // runs OK
m.#b = 0; // error - private property cannot be modified outside class
*Note: there's no way to define private methods, gettersand setters.
*注意:无法定义私有方法、getter和setter。
but a proposal is there https://github.com/tc39/proposal-private-methods