Animal `this.type`
//
function Animal(type){
this.type = type;
}
Animal.isAnimal = function(obj, type){
if(!Animal.prototype.isPrototypeOf(obj)){
return false;
}
return type ? obj.type === type : true;
};
function Dog(name, breed){
// ↓
// Animal.call(this, "dog");
this.name = name;
this.breed = breed;
}
Object.setPrototypeOf(Dog.prototype, Animal.prototype);
Dog.prototype.bark = function(){
console.log("ruff, ruff");
};
Dog.prototype.print = function(){
console.log("The dog " + this.name + " is a " + this.breed);
};
Dog.isDog = function(obj){
return Animal.isAnimal(obj, "dog");
};
var sparkie = new Dog("Sparkie", "Border Collie");
// true
document.write('Dog.isDog(sparkie): ' + Dog.isDog(sparkie));
`super(hoge)` Javascript `Function.prorotype.call()` `new Animal("dog")` `this`