当前位置: 首页 > 文档资料 > Lazy.js 英文文档 >

watch

优质
小牛编辑
121浏览
2023-12-01

Watches for all changes to a specified property (or properties) of an object and produces a sequence whose elements have the properties { property, value } indicating which property changed and what it was changed to.

Note that this method only works on directly wrapped objects; it will not work on any arbitrary ObjectLikeSequence.

Signature

ObjectLikeSequence.watch = function(propertyNames) { /*...*/ }
ObjectLikeSequence.watch = function watch(propertyNames) {
  throw new Error('You can only call #watch on a directly wrapped object.');
}
NameType(s)Description
propertyNamesstring|Array?

A property name or array of property names to watch. If this parameter is undefined, all of the object's current (enumerable) properties will be watched.

returnsSequence

A sequence comprising { property, value } objects describing each change to the specified property/properties.

Examples

var obj = {},
changes = [];

Lazy(obj).watch('foo').each(function(change) {
  changes.push(change);
});

obj.foo = 1;
obj.bar = 2;
obj.foo = 3;

obj.foo; // => 3
changes; // => [{ property: 'foo', value: 1 }, { property: 'foo', value: 3 }]