为什么我可以访问 class 的私有成员?
Why am I able to access private member of the class?
我的代码如下
@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);
为什么我可以在视图中访问名称并显示名称,前提是我已为其指定私有关键字
隐私在 TypeScript 中通常不强制执行,而仅由静态工具检查。
您可以将视图中的绑定和组件 class 视为一个整体,就像模板中的绑定是 class 实现的一部分一样。
对于离线模板编译器,我假设这实际上是生成代码的组织方式。
因为一天结束时 javascript。 Javascript 语言没有 private
关键字。
如果你愿意尝试:
class A {
private x: number;
}
let a = new A();
console.log(a.x);
你会得到一个编译错误:
Property 'x' is private and only accessible within class 'A'
我怀疑(因为我不是 angular 开发人员)你没有得到的原因是你在字符串中有 hero1.name
所以打字稿编译器没有' 将 hero1
视为变量。
我敢打赌,如果你尝试的话:
let obj = new heross("name", "details");
console.log(`heross.name: ${ obj.name }`);
然后你会得到编译错误。
区别是 ${}
而不是 {{}}
.
如果你问为什么它在运行时可以访问,那是因为 javascript 中没有可见性的概念,它没有通过编译器。
编辑
angulardouble curly brace({{ }}
)有区别:
The double curly brace notation {{ }} to bind expressions to elements
is built-in Angular markup
您不需要放入价格变动中,您可以使用常规 single/double 引号。
和 javascript template literals (${ }
):
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
更简单:
let x = 4;
console.log(`x={{ x }}`); // outputs 'x={{ x }}'
console.log(`x=${ x }`); // outputs 'x=4'
我的代码如下
@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);
为什么我可以在视图中访问名称并显示名称,前提是我已为其指定私有关键字
隐私在 TypeScript 中通常不强制执行,而仅由静态工具检查。
您可以将视图中的绑定和组件 class 视为一个整体,就像模板中的绑定是 class 实现的一部分一样。
对于离线模板编译器,我假设这实际上是生成代码的组织方式。
因为一天结束时 javascript。 Javascript 语言没有 private
关键字。
如果你愿意尝试:
class A {
private x: number;
}
let a = new A();
console.log(a.x);
你会得到一个编译错误:
Property 'x' is private and only accessible within class 'A'
我怀疑(因为我不是 angular 开发人员)你没有得到的原因是你在字符串中有 hero1.name
所以打字稿编译器没有' 将 hero1
视为变量。
我敢打赌,如果你尝试的话:
let obj = new heross("name", "details");
console.log(`heross.name: ${ obj.name }`);
然后你会得到编译错误。
区别是 ${}
而不是 {{}}
.
如果你问为什么它在运行时可以访问,那是因为 javascript 中没有可见性的概念,它没有通过编译器。
编辑
angulardouble curly brace({{ }}
)有区别:
The double curly brace notation {{ }} to bind expressions to elements is built-in Angular markup
您不需要放入价格变动中,您可以使用常规 single/double 引号。
和 javascript template literals (${ }
):
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
更简单:
let x = 4;
console.log(`x={{ x }}`); // outputs 'x={{ x }}'
console.log(`x=${ x }`); // outputs 'x=4'