The examples in this article describe the way JavaScript uses prototype to implement object-oriented methods. Share to everyone for your reference. The specific analysis is as follows:
Prototype is a property of a Function object that points to another object. All of the properties and methods of this object are inherited by the instance of the constructor.
At the same time, there is a reference constructor to the constructor, which prototype the prototype chain structure of circular reference successfully.
We can save memory overhead by defining the invariant properties and methods directly on the prototype object.
function Cat (name, color) {
this.name = name;
This.color = color;
}
Cat.prototype.type = ' mammal ';
Cat.prototype.eat = function () {
console.log (' eat fish ');
var cat1 = new Cat (' Kitty ', ' White ');
var cat2 = new Cat (' Smokey ', ' black ');
Console.log (Cat1.type); Mammal
console.log (Cat1.eta = = Cat2.eta);
True, same reference
console.log (cat1.constructor = = Cat)
/TRUE, from Person.prototype
I hope this article will help you with your JavaScript programming.