Several common ways to create JavaScript

Source: Internet
Author: User

1. Create a single object by using the object constructor or the literal

Create an object using the literal method: var stut = {name: "Zhang San"};

Create objects using built-in constructors: var stu = new Object (); stu.name = "Zhang San"

2. Factory mode

The class could not be created before considering ES6, and the developer invented a function that encapsulates the details of creating an object in a particular interface, creating a good object within a function, and then returning the object

The function Createperson () can construct a person object that contains all the necessary information according to the accepted parameters, although the factory pattern solves the problem of creating multiple similar problems but does not solve the object recognition problem

3. Constructor mode

Primitive constructors like object and array are automatically present in the execution environment at run time, and custom constructors can be created to define the type properties and methods of custom objects

Compared to the Factory mode 1. Create object not shown 2. The property method is paid directly to the This object 3. No Return statement

4. Prototype mode

Each function we create has a prototype property, and prototype is the prototype object of the object instance created by invoking the constructor

(1) He omitted the process of passing initialization parameters for the constructor, and all instances would get the same property value by default, although this would be a certain inconvenience to some extent, but this is not the biggest problem, the biggest problem is determined by the nature of their sharing

(2) A property that contains a base property value hides the property in the prototype and then contains the value of the reference data type, which can cause problems

Several common ways to create JavaScript

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.