The following code logs 'true' 4 times:
var menuItem = {
name: "New menuItem",
};
function food(name) {
if(name) this.name = name;
//this.resourceType = "food";
}
food.prototype = menuItem;
function drink(name) {
if(name) this.name = name;
//this.resourceType = "drink";
}
drink.prototype = menuItem;
var burger = new food();
var coke = new drink();
console.log(buger instanceof food);
console.log(burger instanceof drink);
console.log(coke instanceof food);
console.log(coke instanceof drink);
However if I comment out the following two lines:
//food.prototype = menuItem;
//drink.prototype = menuItem;
Then the code does what I want and logs true for "buger instanceof food" and "coke instanceof drink".
Is there a way of checking if the menuItem is an instance of either food or drink correctly while they both share the same prototype (menuItem)?
I'd prefer not to have to store the type like so:
this.resourceType = "food";
If there is a better way of doing it.