简体   繁体   English

Javascript:对象所有属性(可能是跨浏览器)的Mozilla .watch()功能

[英]Javascript: Mozilla .watch() functionality for all properties of an object (possibly cross-browser)

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Object/watch https://developer.mozilla.org/zh-CN/Core_JavaScript_1.5_Reference/Objects/Object/watch

The .watch() method does this in short: "Watches for a property to be assigned a value and runs a function when that occurs." .watch()方法简短地说明了这一点:“监视要为属性分配值的值,并在出现该值时运行一个函数。”

Long descriptive form: "Watches for assignment to a property named prop in this object, calling handler(prop, oldval, newval) whenever prop is set and storing the return value in that property. A watchpoint can filter (or nullify) the value assignment, by returning a modified newval (or by returning oldval)." 长描述性形式:“监视在此对象中分配给名为prop的属性的情况,每当设置prop时都调用处理程序(prop,oldval,newval)并将返回值存储在该属性中。观察点可以过滤(或使该值赋值无效) ,则返回修改后的newval(或返回oldval)。”

There is a question for getting it to work in all browsers here: Object.watch() for all browsers? 这里有一个使它在所有浏览器中都能正常工作的问题:所有浏览器的Object.watch()?

I am looking for something similar to that. 我正在寻找类似的东西。 What I'm looking for is a method I can use to fit this specification: "Watches for assignment to any property in this object and runs a function when that occurs." 我正在寻找一种可用于满足该规范的方法:“监视分配给该对象中任何属性的操作,并在该属性发生时运行一个函数。” The main difference is that it is any property, and just any specific property. 主要区别在于它是任何属性,而只是任何特定属性。

Can somebody create such a method or if they know such a method is already in existence, link to it? 有人可以创建这样的方法吗?如果他们知道这样的方法已经存在,可以链接到它吗? It'd be useful to have it work in all browsers (minus IE, or minus IE8 if IE9 conforms) 使它在所有浏览器中都有用(减IE,或如果IE9兼容,则减IE8)。

Edit: For an example of what I mean, I'll show what I need it for. 编辑:作为我的意思的示例,我将展示我需要的东西。

var DiscreteLine = function (leftBound, length){
  this.positive = [];
  this.negative = [];
  this.length = length;
  this.leftBound = leftBound;
  this.rightBound = leftBound + length

  if (this.leftBound < 0){
    this.negative.length = (leftBound * -1) + 1;
  } else {
    this.negative.length = 0;
  }
  if (this.rightBound >= 0){
    this.positive.length = rightBound + 1;
  } else {
    this.positive.length = 0;
  }

  this.watchObject = new ObjectWatcher(handler(prop, oldval, newval){ /* some stuff */ });

}

Then, when for example, if somebody did the following: 然后,例如,当有人进行以下操作时:

theLine = new DiscreteLine(-2, 4);
theLine[-8] = 10;

The handler would call, with the arguments ("-8", undefined, 10). 处理程序将使用参数(“ -8”,未定义,10)进行调用。 (What would end up happening is, is that the script would recalculate leftBound and length properties automatically (like how Arrays automatically update the length property). (最终会发生的是,脚本将自动重新计算leftBound和length属性(例如Array如何自动更新length属性)。

This would basically require overriding the setter for a property of an already defined object. 基本上,这将需要覆盖已定义对象的属性的设置器。 As far as I know, this is only possible in ECMAScript 5 (via Object.defineProperty ) and at the moment, I'm not sure which browsers support this, if at all, so it wouldn't be "cross-browser" as you had asked. 据我所知,这仅在ECMAScript 5中是可行的(通过Object.defineProperty ),目前,我不确定哪个浏览器支持(如果有的话),因此不会像“跨浏览器”那样你问过了

Edit: Your example makes your requirement clear now. 编辑:您的示例使您的要求现在变得清晰。 I'm afraid the language doesn't provide any way to be notified when new properties are added to an object. 恐怕在将新属性添加到对象时,该语言无法提供任何通知方式。 In your example, you really don't have any choice except to replace the array notation with a function call. 在您的示例中,除了用函数调用替换数组符号外,您实际上别无选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM