Several object creation Modes

Source: Internet
Author: User

Generally, the following two methods are used to create an object:

New object:

VaR person = new object (); person. Name = "Zhang San"; person. Age = "18"; person. Job = "123 ";

Or, the object literal is:

var Person = {    name:"lisi",    age:"18",    job:"123"}

The disadvantage of the two methods is that the same interface creates many objects and generates a large number of repeated code, such as VAR person1 ={}, var person2 ={}. To solve this problem, people began to use a variant of the factory pattern to create objects.

I. Factory Model

Since classes cannot be created in ecmascript, developers have invented a function that encapsulates the details of object creation with specific interfaces, such:

function creatPerson(name,age,job){    var a = new Object();    a.name = name;    a.age = age;    a.job = job;    return a;}
var person2 = creatPerson("lisi","17","456");

To create an object in factory mode, you only need to call the creatperson () function and input relevant parameters no matter how many objects are created. However, the factory mode also leaves a problem, that is, it is impossible to distinguish the type of the object to be created. constructors in ecmascript can be used to create specific types of objects.

Ii. constructor Mode

function Person(name,age,job){    this.name = name;    this.age = age;    this.job = job;    this.sayName = function(){        alert(this.name);    }}var person1 = new Person("lisi","18","123");

Create a custom constructor to define the attributes and methods of the custom object type. A constructor is also a function. The only difference between a constructor and a common function is that the calling method is different. Any function can be called using the new operator as a constructor.
Disadvantage: The function is an object. The sayname is an anonymous function object. Each time a constructor is called for instantiation, an anonymous function object is created. Functions of the same name on different instances are not equal and occupy the memory.

Iii. Prototype

Understanding the prototype: once a new function is created, a prototype attribute is created for the function based on a specific set of rules. This attribute points to the prototype object of the function.
By default, all prototype objects automatically obtain a constructor attribute, which contains a pointer to the function of the prototype attribute.

Function person () {} person. prototype = {constructor: person, name: "Zhang San", age: "18", job: "123", friends: ['xiaoming ', 'xiaogang'], sayname: function () {alert (this. name) ;}} var person1 = new person ();

Here, the person. prototype is set to a new object created in the form of an object literal. because each time a new object is created, its prototype object is also created,
This object will also automatically obtain the constructor attribute. This constructor points to the newly created object instead of the person object. In order to direct it to person,
Constructor: person needs to be added

Disadvantages of prototype:

VaR p1 = new person (); var P2 = new person (); p2.name = 'Li si'; p1.friends. push ('small red'); // point to the same friends array. The friendsconsole in the prototype is modified. log (p1.friends); // ["Xiao Ming", "Xiao Gang", "Xiao Hong"] Console. log (p2.friends); // ["Xiao Ming", "Xiao Gang", "Xiao Hong"] Console. log (p1.friends = p2.friends); // true

Disadvantage 1: the process of passing initialization parameters for the constructor is omitted. As a result, all instances obtain the same attribute value by default.

Disadvantage 2: disadvantages of native Constructor (object, array, etc:

The friends of P1 and the P2 DS of P2 are the same, because P1 modifies the friends of the prototype object, but what we need is that they should not be the same,
Solution: Define the attributes or methods to be shared in the prototype, and define those that do not need to be shared in the constructor, such as friends.

4. Combined use of constructor and prototype

The constructor mode is used to define instance attributes, while the prototype mode is used to define methods and shared attributes. As a result, each instance has its own copy of the Instance attribute, but it also shares the reference to the method, saving the memory to the maximum extent.

Function person (name, age, job) {This. name = Name; this. age = age; this. job = job; this. friends: = ['xiaoming ', 'xiaogang'],} person. prototype = {constructor: person, sayname: function () {alert (this. name );}}

 

 

 

Several object creation Modes

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.