JS Object factory functions and constructors

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/Jener/p/5920963.html

★ Overview:

Using object literals, or adding new members dynamically to an empty object, is the easiest way to create an object. However, in addition to these two commonly used object creation methods, JavaScript also provides other ways to create objects. ★ Create an object using the factory function we can write a function whose function is to create an object that can be called an object factory method. ★ Define Object "construct" function note point: object constructor letter capitalized; Use the This keyword to add members to an object; Use the new keyword to call an object constructor ★ object's constructor property creates an object using the object constructor. The constructor property of each object references this constructor, as shown by using the Object factory function to create the object, the constructor property of each object refers to object (), and the constructor constructor called in the normal way is actually a function. The difference is that when you call it, you must add a "new" keyword, and if you do not add the keyword, the call to it is considered a normal function call. Note that the normal call constructor here, in the output window.name, is the output with the current normal call function value.  If you don't understand, look at the picture. The difference between an object constructor and an object factory Method 1. Object creation code not shown in the object constructor; 2. The properties and methods that the new object should have are added by the this reference; 3. There is no return statement in the object constructor; 4. Usually the first letter of the object constructor is set to uppercase, To distinguish common functions.

JS Object factory functions and 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.