1, the method written in the prototype than write in the constructor consumes less memory, because in the memory of a class prototype only one, the behavior written in the prototype can be shared by all instances, the instance will not be copied in the memory of the instance of a copy
The method that is written in the class, when instantiated, is duplicated in each instance, so the memory consumed is higher
So for no special reason, we generally write attributes into the class, and the behavior is written into the prototype.
2. The properties and methods defined in the constructor are higher than the attributes and methods defined in the prototype, and if the properties and methods of the same name are defined, the constructors will overwrite the
The code is as follows:
<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Untitled Document</title></Head><Body><Scripttype= "Text/javascript">//writing the method in the prototype consumes less memory than writing in the constructor, because there is only one prototype in memory, and the behavior that is written in the prototype can be shared by all instances and is not duplicated in the instance's memory .//The method that is written in the class, when instantiated, is duplicated in each instance, so the memory consumed is higher//so for no special reason, we generally write attributes into the class, and the behavior is written into the prototype .functionABC (name) { This. Name=name; //The properties and methods defined in the constructor are higher than the attributes and methods defined in the prototype, and if the properties and methods of the same name are defined, the constructor will overwrite the prototype, so the following X.sya () pops up "My name is Xudeming", if you remove this sentence, The pop-up was "My name is XX"; //the priority of the method is also the same /*this.say=function () {alert (1); }*/}abc.prototype={name:"xx", say:function() {alert ("My name is"+ This. Name); }}varx=NewABC ("xudeming"); X.say ()</Script></Body></HTML>
Reprinted from: http://www.css119.com/archives/1446
The differences between the properties and methods defined by the JavaScript constructor class and the prototype prototype