Javascript string is a prototype function? -
i creating little shape class in javascript canvas kill time. wondering if below,
var shape = function (shape) { // pseudo code if (shape prototype function of shape) { shape(); } } shape.prototype.quad = function () { } so above valid string quad prototypal function defined.
is possible?
given shape string, use in see if exists on shape.prototype
var shape = function (shape) { if (shape in shape.prototype) { shape.prototype[shape](); } }; this of course doesn't give useful this value in shape.prototype.quad, can't tell want there.
if meant constructor function, you'd use this instead.
var shape = function (shape) { if (shape in this) { this[shape](); } }; if want make it's function, use typeof.
if ((shape in this) && typeof this[shape] === "function") { this[shape](); }
Comments
Post a Comment