在 angular2 中使用 getter 和 setter 的目的是什么? [英] What is the purpose of using getters and setters in angular2?

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

问题描述

我是 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'); }

为什么上面的行与此不同

why is the above line any different from this

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

有时需要允许访问返回一个属性的属性动态计算的值,或者您可能想要反映不需要使用显式方法的内部变量调用.在 JavaScript 中,这可以通过使用吸气剂.

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 视为动态类属性.

更新:

对于您更新后的帖子中的示例,确实如此,它们给出了相同的结果,这是一件好事!您可以同时使用两者,但由于它们的工作方式不同,因此在某些情况下您可以有更多选择.它不像只有一个或只有另一个,或者哪个是最好的.在大多数情况下,您可以同时使用两者,这是您需要使用它们的地方和用途.大多数时候,它是一个被使用的方法,因为它有更全面的用途:我们使用带参数或不带参数的方法来触发对对象的操作.但在某些情况下,它又不会具有与 getter 相同的灵活性.如果您对使用哪个方法犹豫不决,请先使用该方法,当您看到它的局限性时,想想 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 可以轻松替换只返回类属性值的方法.

<小时>更新 2:

get 的另一个好"示例.组件需要检查用户是否已登录以显示/隐藏某些按钮的情况.您无需订阅更改,而是:

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 中使用 getter 和 setter 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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