3 ms·
If you set a function's 'prototype' property to a new object, it's a good practice to set the object's 'constructor' property back to the function, so Tria
by praptak 16y ago
If you set a function's 'prototype' property to a new object, it's a good practice to set the object's 'constructor' property back to the function, so
Triangle.prototype = new Shape(3, 3);
should be followed by:
Triangle.prototype.constructor = Triangle
- regularfry 16y agoWhy is this?
- praptak 16y agoThat's the default on any function you create in JS. This prints 'true': var f = function() {}; document.write(f.prototype.constructor == f); This alone isn't perhaps a very important reason to keep this invariant, but it makes some basic reflection possible. This is (among other things) useful for debugging. It's just sometimes useful to be able to check the problematic object's "class", or at least something as close to class as you can get in a classless language.