Object.observe
polyfill based on EcmaScript 7 spec. Read the documentation for more detailed informations. Check the changelog to see what's changed.
For a polyfill for Array.observe
, you can find something here.
Object.observe
isn't a proposed spec anymoreYou might have read this around, but back in November Object.observe
proposal was withdrawn from TC39. This also means that Object.observe
will be pulled from Chrome and other V8-based environments, and that would imply that developers shouldn't rely on it anymore. Web development evolved in the direction of functional programming and immutable objects, so that's where we all should look at.
You can find the discussion here. V8's development on Proxy
objects is still going, though, as the proposal has been finalized for ES2015.
Regarding this polyfill, its development can be considered concluded. There is still a minor bug that allows to observe the global object, but I don't think I'll address it. In the meanwhile, this package (along with my polyfill for Array.observe
) is now marked as deprecated on the npm
registry.
For the future, I might extract the observing engine from this polyfill and serve it as project on its own, without the legacy of Object.observe
interface. That could still be useful, especially for debugging purposes.
This polyfill extends the native Object
and doesn't have any dependencies, so loading it is pretty straightforward:
<script src="https://github.com/MaxArt2501/object-observe/raw/master/object-observe.js"></script>
Using bower:
$ bower install object.observe
Or in node.js:
$ npm install object.observe
That's it. If the environment doesn't already support Object.observe
, the shim is installed and ready to use.
For client side usage, if you only need the "light" version of the polyfill (see the documentation), replace object-observe.js
with object-observe-lite.js
. You can also use the minified versions of the same files (replacing .js
with .min.js
). If you want to use the "light" version on the server side, you can either directly reference the desired version with require
(as in require("./node_modules/object.observe/dist/object-observe-lite.js");
) or change the "main"
reference in package.json
.
For more details about loading the polyfill on a client, read the documentation.
Because properties are polled, when more than one change is made synchronously to the same property, it won't get caught. This means that this won't notify any event:
var object = { foo: null };
Object.observe(object, function(changes) {
console.log("Changes: ", changes);
});
object.foo = "bar";
object.foo = null;
Object.prototype.watch
could help in this case, but it would be a partial solution.
Property changes may not be reported in the correct order. The polyfill performs the checks - and issues the notifications - in this order:
"add"
and "update"
, or "reconfigure"
"delete"
or "reconfigure"
"preventExtensions"
"setPrototype"
This means that the "add"
change is listed before the "delete"
in the following snippet:
var obj = { foo: "bar" };
Object.observe(foo, ...);
delete obj.foo;
obj.bar = "foo";
Due to the nature of the shim, there's nothing that can be done about it.
Environments that don't support Object.getOwnPropertyNames
(most notably, Internet Explorer prior to version 9) can't detect changes to non-enumerable properties:
var re = /some regex/g;
Object.observe(re, ...);
re.lastIndex = 10;
// Nothing happens...
There's no way to prevent this limitation. Developers that need to support those environments should be careful about observing non-plain objects. A special polyfill is provided for arrays and the length
property.
Although the polyfill can work on DOM nodes or other host objects, the results may be impredictable. Moreover, in older environments like IE8-, observing nodes can be a cumbersome and memory hogging operation: they have a lot of enumerable properties that Object.observe
should not check. Just don't observe nodes: it's not the point of Object.observe
.
Possible memory leaks: remember to unobserve
the objects you want to be garbage collected. This can be avoided with native implementations of Object.observe
, but due to the fact that in this polyfill observed objects are held in internal maps, they can't be GC'ed until they're unobserved. (WeakMap
s could solve this particular issue, but of course they're not for every environment - a shim for Map
is used when not supported - and it's also not possible to iterate through their entries.)
Dirty checking can be intensive. Memory occupation can grow to undesirable levels, not to mention CPU load. Pay attention to the number of objects your application needs to observe, and consider whether a polyfill is actually good for you.
This polyfill has been tested (and is working) in the following environments:
It also does not overwrite the native implentation in Chrome 36+, Opera 23+, node.js 0.11.13+ and io.js.
Some benchmarks have been done on the dirty checking loop. See the docs for more details.
The MIT License (MIT)
Copyright (c) 2015 Massimo Artizzu (MaxArt2501)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.