[concept] JavaScript constructors and normal functions

Source: Internet
Author: User
Tags naming convention

[concept] JavaScript constructors and normal functions

What is the difference between a JavaScript constructor and a normal function:

    1. Differences in invocation modes:
      1. The constructor needs to be called with the new operator, if the constructor has no arguments, you can omit the parentheses, such as new Object.
      2. A call to a normal function does not require the new operator, and it must have parentheses. The role of new can be found in a brief introduction to the role of JS's new operator.
    2. The point of this issue:
      1. The This of the constructor is bound to the created object instance.
      2. This of the normal function belongs to the caller of this function.
    3. Naming method:
      1. Constructor names usually have a larger initial letter.
      2. The normal function name is lowercase, using the Hump naming method. constructor function

To create an object using the constructor

In JavaScript, the constructor is actually a normal function. When you use the new operator to function, it can be called a construction method (constructor).

My understanding: constructors are classes. The prototype of the constructor extends the instantiated method. Constructors can be called new constructors only when called by the operator. Other times, it is a normal function. The particularity of this constructor is not so much the function of the new operator-creating the object and returning the object itself.

The object created by the constructor is no different from the normal var object = {}.

In JavaScript, the general function and the constructor function are compared with the ordinary function, the constructor has the following obvious characteristics
    1. Called with the New keywordvar prince=new Prince("charming",25);
    2. Keywords can be used inside a function this
      Inside the constructor, the this new object that is constructed is pointed to. thisa defined variable or function/method is an instance variable or an instance function/method. You need to use an instance to access it, and you cannot access it with a type name. PRINCE.AGE;//25 prince.age;//undefined
    3. The default return value constructor does not need to return the value explicitly, and is returned by default, which is the this new instance object. Of course, you can also use the return statement, the return value will vary according to the type of return value, details will be described below.
    4. The name of the function is suggested in uppercase and separated from the normal function area. Not in the naming convention, but it is recommended to write this.
What happens when I instantiate with the new keyword?

The Prince () function in the above article is a chestnut:

    1. The first step is to create an empty object.var prince={}
    2. In the second step, the this in the constructor Prince () points to the newly created object prince.
    3. The third step is to point the _proto_ property of Prince to the prototype of the Prince function and create the relationship between the object and the prototype.
    4. Fourth step, execute the code within the constructor prince ().
What if the constructor has a return value?

When return is not explicitly called in the constructor, the default is to return the This object (which must be new called using the keyword), which is the newly created instance object. When you call return in a constructor, there are two things:

    1. returnare five simple data types: string,number,boolean,null,undefined. In this case, ignoring the return value still returns the this object.

    2. returnis an object in this case, the this object is no longer returned, but instead returns the return value of the return statement.

    function person (name) {        this.name=name;        return {name: "Cherry"}    }    var person=new person ("Sheila");    Person.name;//cherry    person;//object {name: "Cherry"}

[concept] JavaScript constructors and normal functions

Related Article

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.