Prototype is a very important concept in Js, each function (in Js the function is also an object) has a property called prototype, that is, the prototype), but under normal circumstances, its value is null, but it has a very important function is that the instances will share its properties and methods (this is the basis for implementing inheritance in Js)! Prototypes are shared for instances of objects, which is both convenient for the program and confusing, with many unexpected results!
[Quote] 1. The object method includes the method in the constructor and the method on the constructor prototype; 2. Class method, in fact, the class here is a function, in js because the function is also an object, so you can add attributes and methods to the function, this method is more used in node; 3. The prototype method is generally used for object instance sharing, such as Person.prototype.sayName=function(){console.log(this.name); }; Add the method to the prototype to enable sharing. This eliminates the need to allocate memory to an instance every time it is initialized.
|