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

查看:91
本文介绍了何时使用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 ,但方法被定义为类 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始终访问包括计算属性的所有属性?如果没有,什么时候可以直接访问属性是安全的?

我绝对希望在这里坚持最佳实践,即使用吸气/ 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. 是的,总是访问具有获取的属性。

  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.

属性已更新并读入一个异步表单,如果您直接使用 访问它们,则不能保证您获得最新的价值。

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. 刷新更改队列

    • 在此之前,该属性实际上已更新(而将照常运行)

  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)

想象你使用这个读取一个属性,但是发生一些改变其值的事件。在刷新队列之前,新值将不可用,但这个会立即读取该属性,并返回计划稍后更新的属性的值。你得到陈旧的数据。 和设置方法为您管理此异步,并始终保证新的值。

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天全站免登陆