0

say i have an array x and a constructor

function Road(footpath,divider,length,lanes){}

and

x[1] = new Road(1,1,1,1);

now how do i determine that x[1] is an object of type Road?

1
  • 1
    use instanceof like x[1] instanceof Road Commented Nov 25, 2014 at 20:18

1 Answer 1

4

Simple like this

if(x[1] instanceof Road) {
    //Hey, you have a road!
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that but it returns false probably because i add more to it like x[1].children ="Something else"; now if i say x[1] instanceof Road it returns false
Adding an attribute to your object doesn't seem to be your issue; have a look at a working example: jsbin.com/zikivobese/2/edit

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.