在angular2中使用方法名称之前使用get的目的是什么? [英] What is the purpose of using get before the method name in angular2?

查看:449
本文介绍了在angular2中使用方法名称之前使用get的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是angular2的新手,当我查看某人的代码时,一条特定的行让我感到困惑

i am new to angular2 and when i was reviewing someone's code, one specific line got me confused

get formData() { return <FormArray>this.lienHolder.get('policyDetails'); }

为什么以上行与此不同

formData() { return <FormArray>this.lienHolder.get('policyDetails'); }

我在谷歌搜索了这个并发现没有实际结果,任何人都可以帮我理解这一点。

I searched about this in google and found no actual results, can anyone help me to understand this.

更新

这个

var obj = { log: 0, get latest() { return this.log++; } }; 

var obj = { log: 0, latest() { return this.log++; } }; 

两者都给我更新的价值我称之为
obj.latest& ; obj.latest() - 一直返回更新的结果然后为什么使用另一个?

both are giving me the updated value all the time i call them obj.latest & obj.latest() -- returns updated result all the time then why use one over another?

推荐答案

get formData()

被称为 getter 访问者。它允许您动态获取属性。它总是应该返回一个值。

is called a getter accessor. It allows you get the property dynamically. It always should return a value.



https://www.typescriptlang.org/docs/handbook/classes.html


TypeScript支持getter / setter作为拦截对象成员访问
的方法。这使您可以对每个对象上的成员访问方式进行更精细的
控制。

TypeScript supports getters/setters as a way of intercepting accesses to a member of an object. This gives you a way of having finer-grained control over how a member is accessed on each object.

https://developer.mozilla.org/en-US/ docs / Web / JavaScript / Reference / Functions / get


有时需要允许访问返回a的属性
动态计算的值,或者您可能希望将
的状态反映为内部变量,而无需使用显式方法
调用。在JavaScript中,这可以通过使用
getter来完成。

Sometimes it is desirable to allow access to a property that returns a dynamically computed value, or you may want to reflect the status of an internal variable without requiring the use of explicit method calls. In JavaScript, this can be accomplished with the use of a getter.






与此相反, getFormDate()函数,它可以接受参数而不总是返回值。


In opposite to that, getFormDate() is a function, it can take arguments and not always returns values.

其中一个案例,我喜欢使用getter是指应该从服务获取属性:

One of the cases, where I like to use a getter is when a property should be get from a service:

<p>{{dictionary.label1}}</p>

然后我从这样的服务中得到它:

and then I get it from a service like this:

get dictionary(){
  return this.myService.getDictionary();
}

这种方式当服务改变数据时我动态地可以接收到我的值绑定/模型。

this way when the service changed the data I dynamically can receive the value to my binding/model.

如果我已经定义如下:

dictionary: [];

ngOnInit(){
  this.dictionary = this.myService.getDictionary();
}

当服务已收到时,我会被旧数据'卡住'新的数据集。当然,您可以设置更改侦听器并触发更新,但它的代码更多!

then I would be 'stuck' with the old data while the service already received the new set of data. Of course, you can then set a change listener and trigger the update, but it's more code!

将getter视为动态类属性。

更新:

对于更新后的帖子中的示例,确实如此,它们会给出相同的结果,这是一件好事!您可以同时使用它们,但由于它们的工作方式不同,因此在某些情况下可以使用更多选项。它不仅仅是一个或只是另一个,或者哪个是最好的。在大多数情况下,您既可以使用它们,也可以使用它们。大多数情况下,它是一种使用的方法,因为它具有更全面的用途:我们使用带或不带参数的方法来触发对象上的操作。但在某些情况下,它不会具有与吸气剂相同的灵活性。如果你犹豫要使用哪一个,首先使用方法,当你看到它的极限时,想想吸气剂是否能帮到你,就像现在你知道它的目的是什么,这是一个属性,但是动态!



另一个例子:

For the examples in your updated post, it's true, they give the same result, and it's a good thing! You can use both, but as they don't work similarly, you can have more options in some cases. It's not like only one or only the other, or which one is the best. In most of cases you can use both, it's where and for what you need to use them. Most of the times, it's a method which is used, as it has more comprehensive use: we use methods with or without parameters, to trigger actions on objects. But in some cases, again, it will not have the same flexibility as a getter. If you are hesitant which one to use, use the method first and when you see its limits, think if the getter would help you, as now you know what's its purpose, which is - a property, but dynamic!


An other example:

isShown:boolean; //is 'static', will return the same value unless you change it in some kind of a method

get isShown(){
   return this.someCondition && this.someMethodResult() || this.anotherCondition
}

如果someCondition和anotherCondition发生变化,someMethodResult的结果必须来改变了你不必请求isShown值,它是动态完成的。

If someCondition and anotherCondition change and the result from someMethodResult had to come changed you don't have to request isShown value, it's done dynamically.

相反,你可以拥有

setShown(){ //the method
  this.isShow = !this.isShown;
}

这里需要调用setShown,因此可以更新isShown。

Here setShown needs to be called so isShown could be updated.

此外,getter可以轻松替换只返回类属性值的方法。



UPDATE2:

获取的另一个好示例。组件需要检查用户是否已登录以显示/隐藏某些按钮的情况。您可以这样做,而不是订阅更改:

An other 'good' example for get. A case when a component needs to check if the user is logged to show/hide some buttons. Instead of subscribing to changes, you do:

HTML:

<button [hidden]="!isLogged">Log out</button>

打字稿:

get isLoggedIn(){
   return this.authService.isLoggedIn();
}

就是这样!如果用户退出,该按钮将立即立即禁用。没有重要的订阅/取消订阅......

And that's it! If the user is logged out the button will be disabled 'immediatly'. No nead for the heavy subscribe/unsubscribe...

这篇关于在angular2中使用方法名称之前使用get的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆