Object.observe - 不是所有主流浏览器都支持,我可以使用什么作为替代? [英] Object.observe -- not supported by all major browsers, what can I use as an alternative?

查看:102
本文介绍了Object.observe - 不是所有主流浏览器都支持,我可以使用什么作为替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能在Chrome中运行,当一个名为finishedLoading的变量改变值时,它会打印到控制台。

I have this function that works in Chrome, which prints to the console when a variable called finishedLoading changes values.

Object.observe(finishedLoading, function(id, oldval, newval) {
         console.log('finished loading' + id + ' went from ' + oldval + ' to ' + newval);
     }

这在一堆其他现代浏览器中不起作用(例如firefox,safari)。有没有我可以使用的替代方案会更好支持?谢谢!

This doesn't work in a bunch of other modern browsers (e.g. firefox, safari). Is there an alternative I can use that would be better supported? Thanks!

推荐答案

更广泛支持的方法可以是 Object.defineProperty defineProperty 可用于对某个属性进行某些控制例如:

A more widely supported approach could be Object.defineProperty. defineProperty can be used to have some control on a certain property of an object for instance:

var o = { prop: '' };
Object.defineProperty(o, 'prop', {
  get: function() { return this.value; },
  set: function(newValue) {
    // a certain property is being changed
    alert('is changed');
    this.value = newValue; 
  }
});

o.prop = 'Johnson';

以上示例显示了如何使用 defineProperty 以及何时 prop 对象<更改了em> o ,调用了定义的setter( set )。

The above example shows how you can use defineProperty and when prop of object o is altered a defined setter (set) is called.

引用你可以看到即使IE-8支持它,但只在某些条件下(IE8只支持在DOM节点上使用Object.defineProperty)。

At the bottom of this reference you can see that even IE-8 supports it but only under certain conditions (IE8 only support Object.defineProperty to be used on DOM nodes).

但是在使用它时要小心,因为这会将一个属性分配给窗口对象,因为缺少这个

But be careful when using it because this would assign a property to the window object as well because of a missing this:

var o = { b:''};
Object.defineProperty(o, 'b', {
  get: function() { return value; },
  set: function(newValue) { value = newValue; },
});

o.b = 'abc';
console.log(window.value); // 'abc'

追踪物业旧价值的方法

这符合您的更多请求:

var o = { prop: '' };

Object.defineProperty(o, 'prop', {
  get: function() { return this.propValue; },
  set: function(newValue) {
    // an certain property is being changed
    console.log('old-value: ',this['oldprop']);
    console.log('new-value: ',newValue);
    this.propValue = newValue; 
    this['oldprop'] = this.propValue;
  }
});

o['prop'] = 'joseph';
console.log(o);
o['prop'] = 'jack';
console.log(o);
o['prop'] = 'john';
console.log(o);

使用Object.defineProperty观察整个对象

除此之外,你还可以创建一个跟踪整个对象的函数以及是否有任何属性被更改:

And in addition to that you could make a function that tracks a whole object and whether any property is being changed:

function observeObject(obj){

  var keys = Object.keys(obj);

  for(var k=0; k < keys.length; k++){

    var key = keys[k];

    (function(key){

      var keyName = key+'value';
      var oldKeyName = 'old'+key+'value';

      obj[oldKeyName] = obj[key];

      Object.defineProperty(obj, key, {
        get: function() { return this[keyName]; },
        set: function(newValue) {

          console.log('old-value: ',this[oldKeyName]);
          console.log('new-value: ',newValue);

          this[keyName] = newValue; 
          this[oldKeyName] = this[keyName];

        }
      });



    })(key);

  }

}


var person = { name : 'jack', age: 26 };

observeObject(person);

person.name = 'john';
person['age'] = 27;

这篇关于Object.observe - 不是所有主流浏览器都支持,我可以使用什么作为替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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