1

I am creating a little Shape class in javascript for canvas to kill some time. I was wondering if I could do something like below,

var Shape = function (shape) {

     // pseudo code
     if (shape is a prototype function of Shape) {
         shape();
     }
}

Shape.prototype.quad = function () {

}

So for the above the only valid string would be quad as that is the only prototypal function defined.

Is this possible?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
GriffLab
  • 2,076
  • 3
  • 20
  • 21
  • This question should be a duplicate of http://stackoverflow.com/q/1098040/218196. Additionally you might want to check whether the property is a function. – Felix Kling Apr 05 '13 at 23:03
  • I tried searching for a relevant question with the same title but nothing came up. My javascript knowledge isn't that great to know it would be related to an array question. Thanks for the heads up about checking if it is a function. – GriffLab Apr 05 '13 at 23:23

3 Answers3

2

Given that shape is a string, just use in to see if it exists on Shape.prototype

var Shape = function (shape) {
     if (shape in Shape.prototype) {
         Shape.prototype[shape]();
     }
};

This of course doesn't give you a useful this value in Shape.prototype.quad, but I can't tell what you want there.


If you meant to do this as the constructor function, then you'd use this instead.

var Shape = function (shape) {
     if (shape in this) {
         this[shape]();
     }
};

If you want also to make certain that it's a function, then use typeof.

 if ((shape in this) && typeof this[shape] === "function") {
     this[shape]();
 }
  • This answers my question, thank you. I am basically making a `Shape` class that people can pass in a string and it would call the relative prototype function. So doing `var shape = new Shape("quad");` would call the function to create a quadrilateral shape. Thank you again! – GriffLab Apr 05 '13 at 23:24
0

jsFiddle Demo

I think what you are looking for is inheritance detection. This can be done by checking instanceof. Here is an example:

var Shape = function (shape) {
 if( shape instanceof Shape ){
  alert("shape instance found");   
 }
};

var Quad = function(){};
Quad.prototype = new Shape();

var q = new Quad();
var s = new Shape(q);

edit

jsFiddle Demo

Perhaps you would like to look for a prototype defined by a string? In that case, do this:

var Shape = function (shape) {
 if( typeof this[shape] == "function" ){
    alert("shape is a prototype function");   
 }
};
Shape.prototype.quad = function(){};

var c = new Shape("circle");
var q = new Shape("quad");
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • Mmh, I interpret *"So for the above the only valid string would be `quad` as that is the only prototypal function defined."* differently. – Felix Kling Apr 05 '13 at 23:07
  • @FelixKling - This was just my best guess, and I thought the example would provide at least some context to what I saw from the psuedo code defined (which looked to me like a check for polymorphism through inheritance detection, i.e. a shape is a Shape because it inherited from Shape). – Travis J Apr 05 '13 at 23:12
  • This is what I thought at first, but his question states that a *string* is being passed in. Is there a better way than doing `eval("Shape.prototype."+shape) instanceof Shape`? :-( – MattDiamant Apr 05 '13 at 23:13
  • I see and understand. I guess we have to wait for some clarification. – Felix Kling Apr 05 '13 at 23:14
  • @Matt: That would always be `false`, since `Shape.prototype.X` is going to be a function (most likely) and not an instance of `Shape`. But instead of `eval("Shape.prototype."+shape)` you should use bracket notation: `Shape.prototype[shape]`. – Felix Kling Apr 05 '13 at 23:14
0

Try this assuming Shape is a constructor, it uses the non-standard but commonly available proto property.

var Shape = function (shape) {
    for (var functionName in this) {
        if (this.__proto__.hasOwnProperty(functionName)) {     
            if (this[functionName]  ===  shape) {
                shape.call(this);
            }
        }            
    }
}

Shape.prototype.quad = function () { console.log("quad")}
new Shape(Shape.prototype.quad)
BeWarned
  • 2,280
  • 1
  • 15
  • 22