The constructor initializes an instance object. the prototype attribute of an object inherits from an instance object. This article will share with you a detailed explanation of javascript constructor. if you are interested in js constructor knowledge, you can learn it. constructor is to initialize an instance object. the prototype attribute of an object inherits an instance object.
Constructor considerations:
1. uppercase letters of default functions
2. the constructor does not show that nothing is returned. The new operator will automatically create a given type and return them. when the constructor is called, new will automatically create this object, and the type is the constructor type.
3. you can also display the call return in the constructor. if the returned value is an object, it will be returned in place of the newly created object instance. If the returned value is of the original type, it is ignored and the newly created instance is returned.
function Person( name){ this.name =name; } var p1=new Person('John');
Equivalent:
function person(name ){ Object obj =new Object(); obj.name =name; return obj; } var p1= person("John");
4. because the constructor is also a function, it can be called directly, but its return value is undefine. in this case, the this object in the constructor is equal to the global this object. This. name is actually a global variable name. In strict mode, an error occurs when you call the Person constructor through the new method.
5. You can also use the Object. defineProperty () method in the constructor to help us initialize:
Function Person (name) {Object. defineProperty (this, "name" {get: function () {return name;}, set: function (newName) {name = newName;}, enumerable: true, // enumerable. the default value is false retriable: true // configurable});} var p1 = new Person ('John ');
6. use the prototype object in the constructor
// More efficient than writing directly in the constructor: Person. prototype. sayName = function () {console. log (this. name );};
However, if there are many methods, most people will adopt a more concise method: directly replace the prototype object with an object literal, as shown below:
Person.prototype ={ sayName :function(){ console.log(this.name); }, toString :function(){ return "[Person "+ this.name+"]" ; } };
This method is very popular, because you do not need to type Person. prototype multiple times, but you must note the following side effects:
Literally, the prototype Object changes the attributes of the constructor, so it points to the Object rather than the Person. This is because the prototype object has a constructor attribute, which is not available to other object instances. When a function is created, its prototype attribute is also created, and the constructor attribute of the prototype object points to the function. When a prototype Object is rewritten in literal form, its constructor attribute is set to a generic Object. to avoid this, you need to manually reset the constructor when rewriting the prototype Object, as shown below:
Person.prototype ={ constructor :Person, sayName :function(){ console.log(this.name); }, toString :function(){ return "[Person "+ this.name+"]" ; } };
Test again:
P1.constructor = Person
True
P1.constructor = Object
False
P1 instanceof Person
True