Constructors in JavaScript

Source: Internet
Author: User

Constructor: Another way to execute a function, create an object after execution, and create a prototype object.

Prototype chain: A pointer to an object access constructor.

Functions: Function object.

The object function: All ancestor objects that create objects are also implemented by Function objects.

Properties of the constructor:

1. When a constructor is declared, the syntax is exactly the same as the normal function,
2. The this pointer is different in the function body
3. Constructors are called differently than normal functions
4. Constructors are typically used to create a class that follows the code specification for C #, capitalized in the first letter.

Run the following code (and trace func):

<! DOCTYPE html>

Effect:

Type of Detection fun.prototype:

View Fun:

Test this with a normal function call (the program does not contain a reference to this, and the undefined appears):

Assign a value to this (it must be referenced in the program body):

Run the following code:

<! DOCTYPE html>

To run the code:

<! DOCTYPE html>

This is in the constructor:

The above is different for this in the normal function and the constructor:

1.this represents the current object in other languages and is very flexible in JS
2.this is related to the running environment of the current function
3. For normal functions, this represents the run environment object that invokes the current function in the function body
4. This represents the object created by the current constructor for the constructor function

5. In the global environment this represents the Global Environment (Window object)

Constructor: The new fun execution process:

1. Create an empty obj object

2. The prototype chain of an empty object (__proto__) points to the function object prototype object

3. Modify the function's this pointer to the newly created object reference.

4. Execute function body (normal function only executes fourth step)

Constructors in JavaScript

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.