TypeScript 中的私有静态属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12827544/
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
Private static properties in TypeScript
提问by MuriloKunze
If I do something like this below, how can I access the property out the class?
如果我在下面做这样的事情,我怎样才能在类中访问该属性?
class Person
{
private static name: string;
}
console.log(Person.name);
Shouldn't it inaccessible?
不是应该无法访问吗?
采纳答案by Brian Terlson
It should be an error but isn't. From the spec, section 8.2.1:
这应该是一个错误,但不是。从规范,第 8.2.1 节:
It is not possible to specify the accessibility of statics—they are effectively always public.
无法指定静态的可访问性——它们实际上始终是公开的。
Accessibility modifiers on statics are something the team has considered in the past. If you have a strong use case you should bring this up on codeplexsite!
静态的可访问性修饰符是团队过去考虑过的事情。如果你有一个强大的用例,你应该在codeplex站点上提出这个!
回答by Alexander Christov
Well, not really, in fact you can. Maybe most important is to ask about the TypeScript versionit relates. I have v1.5 beta, part of my VS2012 installation (yes, it works despite it is targeted at VS2013).
嗯,不是真的,事实上你可以。也许最重要的是询问它相关的 TypeScript版本。我有v1.5 beta,它是我 VS2012 安装的一部分(是的,尽管它是针对 VS2013 的,但它仍然有效)。
I have a class like this:
我有一个这样的课程:
class ItemListPreProcessor {
private static names: string[] = [ 'Name', 'Age' ];
static createHeader = (eltName: string) => {
var pdiv = $(eltName);
pdiv.html('<table><thead><tr></tr></thead></tr><tbody></tbody></table>');
var row = $('tr', pdiv);
ItemListPreProcessor.names.forEach((n) => {
row.append('<th>' + n + '</th>');
});
return $('tbody', pdiv);
};
}
In the sample above you can see both privateand static. The class is compiled to the following JavaScript:
在上面的示例中,您可以同时看到private和static。该类被编译为以下 JavaScript:
var ItemListPreProcessor = (function () {
function ItemListPreProcessor() {
}
ItemListPreProcessor.names = ['Name', 'Age'];
ItemListPreProcessor.createHeader = function (eltName) {
var pdiv = $(eltName);
pdiv.html('<table><thead><tr></tr></thead></tr><tbody></tbody></table>');
var row = $('tr', pdiv);
ItemListPreProcessor.names.forEach(function (n) {
row.append('<th>' + n + '</th>');
});
return $('tbody', pdiv);
};
return ItemListPreProcessor;
})();
and there's no problem with either compiling it (this you see), as well as executing it (this you should trust, or, if you like, try).
编译它(你看到的)和执行它(你应该相信,或者,如果你愿意,可以尝试)都没有问题。
回答by tony
class Person
{
private static theName: string = "John";
static get name():string{
return Person.theName;
}
}
console.log(Person.name);
If a static property is private we need to provide a static get method to access it. This may not be a common solution but it is the only way I know of to directly access a private static property. On the other hand, you may have to add a second get method if you also intend to access the property from an instantiated object. Both get methods can have the same name since the static get method will be invisible to the instantiated object.
如果静态属性是私有的,我们需要提供一个静态的 get 方法来访问它。这可能不是一个常见的解决方案,但它是我所知道的直接访问私有静态属性的唯一方法。另一方面,如果您还打算从实例化对象访问该属性,则可能必须添加第二个 get 方法。两个 get 方法可以具有相同的名称,因为静态 get 方法对实例化对象不可见。

