Home > Web Front-end > JS Tutorial > body text

Detailed explanation of JavaScript constructor_javascript skills

WBOY
Release: 2016-05-16 15:23:11
Original
1686 people have browsed it

The constructor is to initialize an instance object, and the prototype attribute of the object is to inherit an instance object.

Constructor Notes:

1. The first letter of the default function is capitalized

2. The constructor does not return anything. The new operator automatically creates the given types and returns them. When the constructor is called, new automatically creates the this object, and the type is the constructor type.

3. You can also call return explicitly in the constructor. If the returned value is an object, it will be returned instead of the newly created object instance. If the returned value is a primitive type, it is ignored and a newly created instance is returned. 

 function Person( name){
        this.name =name;
      }
       var p1=new Person('John');
Copy after login

is equivalent to:

   function person(name ){
        Object obj =new Object();
        obj.name =name;
         return obj;
      }
       var p1= person("John");
Copy after login

4. Because the constructor is also a function, it can be called directly, but its return value is undefined. At this time, the this object in the constructor is equal to the global this object. this.name actually creates a global variable name. In strict mode, an error occurs when you call the Person constructor via new.

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, //可枚举,默认为false
           configurable:true //可配置
         });
      }  
       var p1=new Person('John');
Copy after login

6. Use prototype objects in the constructor

 //比直接在构造函数中写的效率要高的多
       Person.prototype.sayName= function(){
         console.log(this.name);
      };
Copy after login

But if there are many methods, most people will adopt a simpler method: directly replace the prototype object with an object literal, as follows:

 Person.prototype ={
        sayName :function(){
           console.log(this.name);
        },
        toString :function(){
           return "[Person "+ this.name+"]" ;
        }
      };
Copy after login
This method is very popular because you don’t have to type Person.prototype multiple times, but there is a side effect you must pay attention to:

Rewriting the prototype object using literal form changes the properties of the constructor, so it points to Object instead of Person. This is because the prototype object has a constructor property, which other object instances do not have. When a function is created, its prototype property is also created, and the constructor property of the prototype object points to the function. When a prototype object is rewritten using object literal form, its constructor property will be set to a generic object Object. In order to avoid this, you need to manually reset the constructor when rewriting the prototype object, as follows:


 Person.prototype ={
        constructor :Person,
        
        sayName :function(){
           console.log(this.name);
        },        
        toString :function(){
           return "[Person "+ this.name+"]" ;
        }
      };
Copy after login
Test again:

p1.constructor===Person


true


p1.constructor===Object


false


p1 instanceof Person


true

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template