为什么getter和setter的骨干机型? [英] Why getters and setters in Backbone's models?

查看:146
本文介绍了为什么getter和setter的骨干机型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有getter和setter与骨干模式?有没有私有/保护属性,所以getter和setter方法​​是封装有点用处。我知道他们是有用的验证,但除此之外,他们有什么效用,如果我们能够绕过它们?

Why are there getters and setters with Backbone models? There are no private/protected attributes, so the getters and setters are a bit useless for encapsulation. I know they are useful for the validations, but other than that, what are their utility if we can bypass them?

推荐答案

设置不是简单地做下面的事情。

set is not simply doing below thing.

this.attributes["key"] = value;

如果您绕过它,你就会错过它是这里做所有其他职能。

If you bypass it, you will miss all other functions that it is doing here.

目前它正在做2功能。

1.It呼吁 _Validate 前值的方法分配给属性。 请参阅本

1.It is calling _validate method before value is assigned to attributes. refer this.

2.It触发器更改事件,如果你听一个属性的变化。

2.It triggers change event if you listen for an attribute change.

如果他们在未来添加更多的功能,你可能会失去他们,如果通过通设置

If they add further functionalities in future, you may lose them, if you by-pass set.

这篇关于为什么getter和setter的骨干机型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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