JavaScript constructor detailed _javascript tips

Source: Internet
Author: User

The constructor is the initialization of an instance object, and the prototype property of the object inherits an instance object.

Constructor Considerations:

1. Default Letter Capital First letter

2. The constructor does not show anything returned. The new operator automatically creates the given type and returns them, and when the constructor is called, new automatically creates the This object and the type is the constructor type.

3. Call return can also be displayed in the constructor. If the value returned is an object, it is returned instead of the newly created object instance. If the returned value is an original type, it is ignored and the newly created instance is returned.

 function person (name) {
        this.name =name;
      }
       var p1=new person (' John ');

Equivalent to:

   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 invoked directly, but its return value is Undefine, at which point the this object within the constructor equals the global this object. THIS.name is actually creating a global variable name. In strict mode, when you make a call to the person constructor via new, an error occurs.

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, default to False
           Configurable:true//Configurable
         });
      }  
       var p1=new person (' John ');

6. Using a prototype object in a constructor

 More
       person.prototype.sayname= function () {
         console.log (this.name) than the efficiency that is written directly in the constructor;
      

But if there are more methods, most people will adopt a more concise approach: Replace the prototype object directly with an object literal, as follows:

 Person.prototype ={
        sayname:function () {
           console.log (this.name);
        },
        tostring:function () { return
           ' [person ' + this.name+ '] ';
        }
      ;

This approach is very popular because you don't have to type person.prototype multiple times, but there is a side effect you must be aware of:

Rewriting a prototype object with a literal form changes the property of the constructor, so he points to object instead of person. This is because the prototype object has a constructor attribute, which is not in other object instances. When a function is created, its prototype property is created, and the constructor property of the prototype object points to the function. When you overwrite a prototype object in the form of an object literal, its constructor property is set to the generic object objects. To avoid this, you need to manually reset the constructor when you overwrite the prototype object, as follows:

 Person.prototype ={
        Constructor:P Erson,
        
        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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.