简体   繁体   English

为什么我可以访问班级的私人成员?

[英]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中没有可见性的概念,所以它不会通过编译器。


Edit 编辑

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.

相关问题 我可以访问 TypeScript 中 class 定义之外的私有变量吗? - I'm able to access private variables outside the class definition in TypeScript? 为什么我不能访问 TypeScript 私人成员? - Why can I access TypeScript private members when I shouldn't be able to? 如何覆盖 TypeScript 中 class 成员的“私有”限定符? - How do I override the `private` qualifier of a class member in TypeScript? 如何从 TypeScript 中相同的 class 的 static 方法访问 class 的私有成员? - How to access private member of a class from a static method of the same class in TypeScript? 为什么无法渲染此图像? - Why am not able to render this? 我无法访问两个对象中的 id - I am not able to access the id inside both objects 我无法使用 nestjs 访问 ytdl-core 函数 - I am not able to access ytdl-core functions with nestjs 为什么我在此React / Typescript应用程序中收到no exported member错误? - Why am I getting the no exported member error in this React/Typescript app? 这个通知“标识符&#39;gridOptions&#39;是指componentAngular的私有成员”告诉我,我没有遵循编码最佳实践? - Is this notice “Identifier 'gridOptions' refers to a private member of the componentAngular” telling me that I am not following coding best practices? 我无法在Ionic2上做出承诺 - I am not able to set my class into a promise on Ionic2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM