0

Lets say I have:

function Pet(){}
Pet.prototype.breathe = function(){}

And

function Cat(){}

I then go along and do:

Cat.prototype = Object.create(Pet.prototype)
Cat.prototype.purr = function(){}

I know I can check

var cat = new Cat()
console.log(cat instanceof Pet); //true

But how can I check if the function Cat is going to be an instance of Pet without instantiating it?

The most simple and hacky way I can think of is...

Pet.prototype.$$pet = true

And then check

console.log(Cat.prototype.$$pet) //true

But that doesn't seem very nice because I can just go along and do

Cat.prototype.$$pet = true

1 Answer 1

1

Turns out you can actually use

Cat.prototype instanceof Pet

MDN's examples of instanceof shows this

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

Sign up to request clarification or add additional context in comments.

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.