使用Javascript的setter和getter属性,以监视更改一个坏主意? [英] Is using javascript setters and getter properties to watch for changes a bad idea?

查看:93
本文介绍了使用Javascript的setter和getter属性,以监视更改一个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的Object.create可以定义在JavaScript setter和getter方法​​。

Using Object.create one can define setter and getter methods in javascript.

o = Object.create(Object.prototype, {

  fooBar: {
    get: function() { return "Meh" },
    set: function(value) { console.log(value) }
  }
});

的console.log(O)总是会返回咩,做 O =Heyo只会输出新值的情况下直接改变 0

console.log(o) will always return Meh, and doing o = "Heyo" would only output the new value directly without changing o.

是否有任何理由,为什么我不应该使用或依赖于这种行为引发的事件?
关于像角和灰烬双向绑定的框架是什么,他们是否使用这个功能呢?
关于单元测试什么?

Is there any reason as to why I should not use, or rely on this behavior to trigger events? What about two-way binding frameworks like angular and ember, do they make use of this feature? What about unit-testing?

什么是看更改触发事件的事实标准的方式,而不使用明确的getter和setter方法​​?

What is the defacto standard way of watching for changes to trigger events, without using explicit setters and getters?

推荐答案

有三种方法来观察JavaScript对象的变化:

There are three ways to observe changes in JavaScript objects:


  1. 方法(如例如你的设置/获取,或组合)

  2. 使用定时器检查属性查询

  3. 观察员来处理它是由浏览器内部处理性能

方法1是常见和简单。如果你想使用的setter /吸气剂是由你。也有使用的选择他们结合:

Method 1 is common and simple. If you want to use setter/getter is up to you. There is also the option of using them combined:

var someValue = 0;

this.myMethod = function(newValue) {

    if (arguments.length === 0) return someValue; /// gets if no arg is given
    someValue = newValue;                         /// sets if arg is given 

    return this;
}

它的清洁和直接的。

It's clean and straight forward.

如果您想改用属性,你需要或者使用轮询使用一个计时器,也可以使用对象的观察者。

If you want to use properties instead you need to either use polling using a timer or you can use an object observer.

不过,对于天体观测当前支持不是很大(我最后一次检查)。 Chrome提供了支持,所以如果你使用那些你需要prepare您code来处理它是不支持的情况下Firefox有自己的观测系统等。这基本上意味着你需要,而不是轮询。同组之方法/获取等是一个更好的办法海事组织。

However the current support for Object observers is not that great (last time I checked). Chrome has support, Firefox has its own observer system and so forth so if you use those you need to prepare your code to handle situations where it isn't supported. This basically means you need to poll instead. Therefor method with set/get etc. is a better approach IMO.

这是观察者与特性非常有用。它可以让你设置一个属性是这样的:

An observer is useful with properties. It will allow you to set a property this way:

myObject.value = 10;

和引发的回调,使您可以处理这种变化。注意它会触发任何属性更改回调所以需要遍历的事件。

and trigger a callback that allows you to handle that change. Note it will trigger callback on any property change so need to iterate the events.

例如:

Object.observe(myObject, observerCallback);

function observerCallback(records) {
    records.forEach(checkRecord);
}

function checkRecord(rec) {

    switch(rec.name) {

        case 'value':
            handleValueChange();
            break;
    }
}

这篇关于使用Javascript的setter和getter属性,以监视更改一个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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