[英]Why am I able to access private member of the class?
My code is as follow 我的代码如下
@Component({
selector: 'my-app',
template: `
<ul>
<li *ngFor = 'let hero1 of heros2'>
{{hero1.name}}
</li>
</ul>
`})
export class AppComponent {
heros2 : any = [
new heross('lee', 'lee'),
new heross('lee1', 'lee1'),
];}
class heross{
private name : string;
constructor(name : string, details : string){
this.name = name;
}}
bootstrap(AppComponent);
why am I able to access name in the view and displaying name, provided that I have given it private keyword 为何我可以在视图中访问名称并显示名称,只要我给它指定了private关键字
If you'll try: 如果您尝试:
class A {
private x: number;
}
let a = new A();
console.log(a.x);
( code in playground ) ( 操场上的代码 )
You'll get a compilation error: 您会收到一个编译错误:
Property 'x' is private and only accessible within class 'A'
属性“ x”是私有的,只能在类“ A”内访问
The reason you're not getting that, I suspect (as I'm not an angular developer), is that you're having hero1.name
in a string so the typescript compiler doesn't treat hero1
as a variable. 我怀疑(因为我不是有角度的开发人员),您没有得到该信息的原因是,您在字符串中
hero1.name
,因此打字稿编译器不会将hero1
作为变量。
I bet that if you try: 我敢打赌,如果您尝试:
let obj = new heross("name", "details");
console.log(`heross.name: ${ obj.name }`);
Then you'll get the compilation error. 然后,您将得到编译错误。
The difference being ${}
instead of {{}}
. 区别是
${}
而不是{{}}
。
If however you're asking why that's accessible at runtime, then that's because there's no notion of visibility in javascript, it doesn't get pass the compiler. 但是,如果您问为什么在运行时可以访问它,那是因为javascript中没有可见性的概念,所以它不会通过编译器。
There's a difference between the angular double curly brace ( {{ }}
): 斜双花括号 (
{{ }}
)之间是有区别的:
The double curly brace notation {{ }} to bind expressions to elements is built-in Angular markup
将表达式绑定到元素的双花括号表示法{{}}是内置的Angular标记
Which you don't need to put into ticks, you can just use regular single/double quotes. 您不需要打勾,只需使用常规的单/双引号即可。
And the javascript template literals ( ${ }
): 和javascript模板文字 (
${ }
):
Template literals are string literals allowing embedded expressions.
模板文字是允许嵌入表达式的字符串文字。 You can use multi-line strings and string interpolation features with them.
您可以使用多行字符串和字符串插值功能。 They were called "template strings" in prior editions of the ES2015 / ES6 specification
在ES2015 / ES6规范的先前版本中,它们被称为“模板字符串”
More simply: 更简单地说:
let x = 4;
console.log(`x={{ x }}`); // outputs 'x={{ x }}'
console.log(`x=${ x }`); // outputs 'x=4'
Privacy is not enforced in TypeScript in general but only checked by static tools. 通常,不会在TypeScript中强制执行隐私保护,而只能通过静态工具进行检查。
You can see the bindings in the view and the component class as a single unit, like the bindings in the template is part of the class implementation. 您可以在视图和组件类中将绑定视为单个单元,就像模板中的绑定是类实现的一部分。
With the offline template compiler I assume this is actually how the generated code will be organized. 使用脱机模板编译器,我认为这实际上就是组织生成的代码的方式。
Because it's javascript at the end of the day. 因为这是一天结束时的JavaScript。 And Javascript language does not have a
private
keyword. 而且Javascript语言没有
private
关键字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.