The case where the method is written in the constructor is referred to as the method within the function, and the case where the method is written on the prototype attribute is referred to as the method on the prototype.
• Methods within the function: Using methods within the function, we can access private variables inside the function. If the object we use the new constructor to require us to operate private variables inside the constructor, we should consider using methods within the function at this time.
• Methods on prototype: When we need to create a large number of objects through a function, and these objects have many methods; at this time, we need to consider adding these methods to the function's prototype. In this case, our code will consume relatively small memory.
•In actual applications, these two methods are often used in combination; so we need to first understand what we need, and then choose how to use it.
// Constructor Afunction A(name) { this.name = name || 'a'; this.sayHello = function() { console.log('Hello, my name is: ' + this.name); }}// Constructor Bfunction B(name) { this.name = name || 'b';}B.prototype.sayHello = function() { console.log('Hello, my name is: ' + this.name);};var a1 = new A('a1');var a2 = new A('a2');a1.sayHello();a2.sayHello();var b1 = new B('b1');var b2 = new B('b2');b1.sayHello();b2.sayHello();I wrote two constructors, the first is A, which contains a method saysHello; the second is constructor B, which writes that method saysHello on the prototype property of constructor B. Writing the method inside the constructor increases the cost of initializing an object through the constructor, and writing the method on the prototype property effectively reduces this cost. You may think that calling methods on the object is much faster than calling methods on its prototype chain, which is not the case. If you do not have many prototypes on the object, their speed is actually similar.
In addition, some things to note:
•First of all, if you define a method on the prototype property of the function, remember that if you change a method, the method of all objects generated by this constructor will be changed.
• Another point is the issue of variable improvement. We can take a look at the following code:
func1(); // An error will be reported here because func1 has not been assigned a value when the function is executed. error: func1 is not a functionvar func1 = function() { console.log('func1');};func2(); // This will be executed correctly because the declaration of the function will be promoted.function func2() { console.log('func2');}• Regarding the issue of object serialization. Attributes defined on the prototype of a function will not be serialized. You can see the following code:
function A(name) { this.name = name;}A.prototype.sayWhat = 'say what...';var a = new A('dreamapple');console.log(JSON.stringify(a));We can see that the output is {"name":"dreamapple"}
The above article briefly discusses the method and prototype of js constructors is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.