JavaScript constructor Parsing

Source: Internet
Author: User

Constructor principles
JavaScript is an object-oriented language, but JavaScript does not have the concept of classes. JavaScript uses constructor to simulate the effect of the class, that is, we use the function to create objects. This also proves that functions play an important role in JavaScript. For example:


Var Book = function (name, price ){
This. name = name;
This. price = price;
}

Var java = new Book ('master Java', 82 );

When the new keyword is used to call the constructor, the execution context changes from the global variable object (window) to an empty context, which represents the new instance. Therefore, this keyword points to the currently created instance

By default, if no content is returned in your constructor, this ---- the current context, that is, the object you have created. Otherwise, any non-original type value is returned. A common function returns undefined if there is no obvious return value.

Create an object using a simulated Constructor
In fact, the process of creating an object using new is not mysterious. We can use code to simulate how to create an object:


Var Book = function (name ){
This. name = name;
};

// Normal usage
Var java = new Book ('master Java ');

// Use code simulation to test in a non-IE browser, which is not supported by IE
Var python = {};
Python. _ proto _ = Book;
Book. call (python, 'Master python ');

Call a constructor as a normal function
In fact, constructor is a common function that can be called as a common function, for example, var result = Book (); of course, the result is undefined. The difference is that this points to the function. Let's re-Modify the above constructor,


Var Book = function (name, price ){
This. name = name;
This. price = price;
If (this = window ){
Return 'name = '+ name + ", price =" + price;
}
}

In this way, when var result = Book ("Java", 100) is executed, the result is "name = Java, price = 100", while var java = new Book ("Java", 100) the result is that a new object is created. Many constructors that come with JavaScript are both common and constructor functions. For example, String and Number. When String and Number are used as constructors,


Var a = new Number (11 );
Var B = newnumber (11.222 );
Var c = new String ("hello ");

When String and Number are used as common functions, they can be used for type conversion. That is, the Number function can convert other types to the number type. The String function can convert other types to strings,


Var a = Number ('11'); // 11
Var B = Number ('hello'); // NaN
Var c = String (11); // '11'
Var d = String (null); // 'null'

Summary
When a function is used as a constructor, the first letter of the function name is often capitalized to indicate that this is a constructor. At the same time, you must use new to call the constructor. In addition, do not use constructors as common functions, because they are easy to produce global variables. If they are not well processed, the return values may easily affect the functions of constructors.

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.