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?

2
  • This question should be a duplicate of stackoverflow.com/q/1098040/218196. Additionally you might want to check whether the property is a function. Commented Apr 5, 2013 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. Commented Apr 5, 2013 at 23:23

3 Answers 3

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]();
 }
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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");

5 Comments

Mmh, I interpret "So for the above the only valid string would be quad as that is the only prototypal function defined." differently.
@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).
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? :-(
I see and understand. I guess we have to wait for some clarification.
@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].
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.