何时使用 getter/setter 以及何时安全地省略它们? [英] When to use getters/setters and when to safely omit them?

查看:13
本文介绍了何时使用 getter/setter 以及何时安全地省略它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据http://emberjs.com/guides/object-model/classes-and-instances/ 需要使用 getter 和 setter 访问属性:

According to http://emberjs.com/guides/object-model/classes-and-instances/ it is required to access properties using getters and setters:

访问对象的属性时,请使用 get 和 set 访问器方法.

When accessing the properties of an object, use the get and set accessor methods.

确保使用这些访问器方法;否则,计算属性不会重新计算,观察者不会触发,模板不会更新.

Make sure to use these accessor methods; otherwise, computed properties won't recalculate, observers won't fire, and templates won't update.

我知道在更改属性时需要使用 setter 来让 Ember 知道更改以便它可以更新绑定,但是读取属性呢?

I understand that it is needed to use setters when changing property to let Ember know about the change so it can update bindings, but what about reading properties?

来自 http://emberjs.com/guides/object-model 的示例/classes-and-instances/

App.Person = Ember.Object.extend({
  say: function(thing) {
    var name = this.get('name');
    alert(name + " says: " + thing);
  }
});

var yehuda = App.Person.create({
  name: "Yehuda Katz"
});

yehuda.say("Yes");

在上面的例子中,this.get('name')用于访问属性name,但是方法say被定义为App.Person 类的一个属性,可以通过点符号直接访问.虽然方法和属性之间存在明显的用法差异,但在 JavaScript 中,两者的实现没有区别.如果我将 this.get('name') 替换为 this.name,该示例仍然有效.

In the example above, this.get('name') is used to access property name, however method say is defined as a property of class App.Person and is accessed directly by dot notation. While there is a distinctive usage difference between method and property, in JavaScript, there's no difference in implementation of both. The example still works if I replace this.get('name') by this.name.

  1. Ember 在对象的方法和属性方面是否存在任何实现差异?
  2. 直接访问方法总是安全的吗?
  3. getter 是否必须始终访问包括计算属性在内的所有属性?如果不是,什么时候可以直接访问属性?

我绝对想在这里坚持最佳实践,即每次都使用 getter/setter,但我想了解 Ember.js 的内部结构 :)

I definitely want to stick to best practise here, which is to use getter/setter every time, but I'd like to understand the internals of Ember.js :)

推荐答案

  1. Ember 中方法或属性的定义与 Vanilla JavaScript 中的相同,但它们在 Ember 中的解析方式不同.您没有使用 POJO.每次从 Route/Controller/View 扩展时,都会添加 Ember 层,以不同方式处理方法和属性.
  2. 不,并不总是安全的.就您而言,这是因为它过于简单.
  3. 是的,始终使用 get 访问属性.
  1. The definition of a method or a property in Ember is the same as in Vanilla JavaScript, but they are resolved different in Ember. You are not working with POJOs. Every time you extend from a Route/Controller/View, you are adding the Ember layer that processes methods and properties differently.
  2. No, is not always safe. In your case it is because it is overly simple.
  3. Yes, always access properties with get.

用户与您的应用的交互会导致您的对象的属性发生变化,我们通过事件处理这些交互.当 Ember 中的一个事件被触发时,它不会立即被解决,而是被放入一个优先级队列并在以后解决.

User interaction with your app is what causes changes in the properties of your objects, we handle those interactions with events. When an event in Ember is fired, it is not resolved immediately, instead it is put in a priority queue and resolved at a later time.

属性以异步形式更新和读取,如果您使用 this 直接访问它们,则不能保证您将获得最新的值.

Properties are updated and read in an asynchronous form, if you access them directly using this there is no guarantee that you'll get the most up to date value.

检查:管理异步

当您对 Ember 中的属性进行更改时,它不会立即传播该更改.相反,它会立即使任何依赖属性无效,但会将实际更改排入队列以待稍后发生.

When you make a change to a property in Ember, it does not immediately propagate that change. Instead, it invalidates any dependent properties immediately, but queues the actual change to happen later.

因此,当您更改属性的值时,大致会发生以下情况:

So, when you change the value of a property, this is roughly what happens:

  1. 属性已更改
  2. 所有依赖属性都失效
  3. 将实际更改排入队列以备后用
  4. 等待当前用户的任何其他事件处理程序完成
  5. 刷新更改队列
    • 在此之前,该属性实际上已更新(this 将照常工作)
  1. A property is changed
  2. All dependent properties are invalidated
  3. Queues the actual change for later
  4. Wait for any other event handlers, for the current user, to finish
  5. Flush the changes queue
    • Until here, the property is actually updated (and this would work as usual)

假设您使用 this 读取一个属性,但发生了其他一些更改其值的事件.在刷新队列之前,新值将不可用,但 this 立即读取该属性并返回计划很快更新的属性的值.你得到陈旧的数据.方法 getset 为您管理这种异步并始终保证新值.

Imagine you use this to read a property, but some other event occurs that changes its value. The new value won't be available until the queue is flushed, but this reads immediately the property and returns the value of a property that is scheduled to be updated soon. You get stale data. The methods get and set manage this asynchrony for you and guarantee fresh values always.

当你只有一个属性时,在整个应用中,这种异步机制是不会被注意到的.

When you have only one property, in the whole app, this asynchronous mechanism won't be noticed.

Ember 中有许多不同的队列,而这一切背后的基本机制是运行循环.

There are many different queues in Ember, and the fundamental mechanism behind all this is the run loop.

这篇关于何时使用 getter/setter 以及何时安全地省略它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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