Given the entity Dog, Cat, Woodpecker, who are the heirs of the essence of Animal. Animal contains the properties age, name, sound, region and the say method. Dog, Cat, Woodpecker does not explicitly contain these properties, but inherits them from Animal. They also contain a goAway method.

Implement the getTypep () function, which takes one of the objects Dog, Cat, Woodpecker and returns its type without using the instanceof operator, but by checking for the presence of properties / methods of the objects.

There are 2 possible solutions, it is to check the constructor object, for example, cat.constructor === Cat, or for Animal to override the toString method, which will return the name of the constructor. But it seems to me that this is not what is requested in the condition.

  • one
    in ES2015, Function a name property added, so obj.constructor.name will be obj.constructor.name . But the task is too non-specific, was any code attached to it? - Grundy
  • Try to write more detailed questions. Explain exactly what you see the problem, how to reproduce it, what you want to get as a result, etc. - Nicolas Chabanovsky
  • Solved the problem in the following way: 'function getType (obj) {if (Cat.prototype.isPrototypeOf (obj)) {return' Cat '; } else if (Dog.prototype.isPrototypeOf (obj)) {return 'Dog'; } else if (Woodpecker.prototype.isPrototypeOf (obj)) {return 'Woodpecker'; }} 'Thanks for the response. - denjke
  • According to the condition, the task most likely implies the use of the "duck" method - if the object behaves like a duck, quacks, walks - then this is a duck !!!!! In other words, it is necessary to verify the specific properties and methods of the object. - pepel_xD

0