admin管理员组文章数量:1195151
I'm new to TypeScript world, and I've seen examples with this to deal with injected objects and set it to a property of the component (this.anything)
First with public and setting by hand to this.nav
export class XPTO {
constructor(public nav: NavController) {
this.nav = nav;
}
}
and this, with private
export class XPTO {
constructor(private nav: NavController) {
//this.nav is nav?
}
}
in both cases after construct the object this.nav is a NavController object. What are the differences of both implementations? Or this is the same when compiled to plain javascript?
I'm new to TypeScript world, and I've seen examples with this to deal with injected objects and set it to a property of the component (this.anything)
First with public and setting by hand to this.nav
export class XPTO {
constructor(public nav: NavController) {
this.nav = nav;
}
}
and this, with private
export class XPTO {
constructor(private nav: NavController) {
//this.nav is nav?
}
}
in both cases after construct the object this.nav is a NavController object. What are the differences of both implementations? Or this is the same when compiled to plain javascript?
Share Improve this question asked Oct 3, 2016 at 21:21 Christian BenselerChristian Benseler 8,0659 gold badges41 silver badges73 bronze badges 2- 1 Possible duplicate of Understanding "public" / "private" in typescript class – Nitzan Tomer Commented Oct 3, 2016 at 21:28
- 1 The linked duplicate doesn't do a good job of answering the question – Ryan Cavanaugh Commented Oct 3, 2016 at 22:43
2 Answers
Reset to default 22Actually in your first example the explicit assignment is not needed at all:
export class XPTO {
constructor(public nav: NavController) {
// This line is not required when we have an access modifier (private/public) on the constructor parameter(s)
// this.nav = nav;
this.someFunction();
}
someFunction(){
console.log(this.nav); // Prints out the NavController.
}
}
Whenever you specify public or private on a constructor parameter a corresponding public/private variable is created on the class and filled with the value of the parameter.
So really, the only difference of the two code samples is that one is private and the other one is public.
The resulting JavaScript will be the same. However, the compiler will throw an error, if you are trying to access private variables in your code.
public
and private
, as a lot of Typescript features, are only TypeScript modifiers. I'm not sure the compiler names these variables exactly the same, but from a JavaScript point of view, the code will be essentially the same.
The interest of Typescript is to give you features like type checking, it doesn't necessarily always modifies the outputted code.
本文标签: javascriptTypeScript use private or public in constructorStack Overflow
版权声明:本文标题:javascript - TypeScript: use private or public in constructor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738513540a2090950.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论