0

How kosher is this?

function X(){

    X.prototype.z = function(){

    };
}

var obj = new X();

according to simple tests, the z function does get assigned the __proto__ of the obj.

what would break because of this call?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • Normal practice. But it would be better in my opinion, as follows: `this.constructor.prototype.z = function()...`. – stdob-- Mar 13 '16 at 02:27
  • well, if this is normal practice, you will at the very least be redefining parts of the prototype every time you call the constructor – Alexander Mills Mar 13 '16 at 02:31
  • But there are some problems: for example, until the creation of the first object method "z" in the prototype is missing. – stdob-- Mar 13 '16 at 02:33
  • I don't believe this is kosher unfortunately, what will happen is that each time you call the constructor, you will overwrite the previous X.prototype.z call, and the value for 'this' will become bound to the new function that is defined, so previously defined constructor calls will have prototype functions set to the wrong context – Alexander Mills Mar 13 '16 at 02:34
  • Okay, maybe you're right. And if so: `if (typeof this.constructor.prototype.method === 'undefined') this.constructor.prototype.method = function () {};` – stdob-- Mar 13 '16 at 02:41
  • Actually, there may be a workaround. Instead of using X.prototype.z, you may be able to this.__proto__.z in the constructor...I am investigating... – Alexander Mills Mar 13 '16 at 04:13

0 Answers0