4 ms·
I messed up my code example for Object.create above rather badly (I haven't done JS in a while), so I'll redo it here: var constructor = function() { }; or
by halo 18y ago
I messed up my code example for Object.create above rather badly (I haven't done JS in a while), so I'll redo it here:
var constructor = function() { };
originalobject = new constructor();
originalobject.prototype = ...;
blah = new constructor();
blah = foo.prototype
In ES5 this can be replaced with:
var constructor = function() { };
originalobject = new constructor();
originalobject.prototype = ...;
boom = Object.create(originalobject);
It's worth adding that the ES5 standard implements getters and setters, although I don't believe anything implements it yet (IE8 implements a subset but only for the global and HTML objects apparently):
var foo = {};
Object.defineProperty(foo, "bar", { set: function(arg) { alert(arg) } }); //replace the built-in "set"
foo.bar = "test"; //alert appears printing "test".
Object.defineProperty(foo, "bar", { set: undefined }); //undo our overloading
foo.bar = "test"; //sets foo.bar to test, as normal