The differences and pros and cons of several JavaScript creation functions.

Source: Internet
Author: User

Factory mode
function createPerson(name,age,job){    var o = new Object();    o.name = name;    o.age = age;    o.job = job;    o.sayName = function(){        alert(this.name);    };    return o;}var person1 = createPerson("tom",20,"trtr");var person2 = createPerson("tom1",24,"httt");

Pros: Use the same interface to create a lot of exclusive, avoid a lot of duplicate code.
Cons: No problem solving object recognition, how to know the type of an object.

Constructor mode

The constructor is to address the drawbacks of the factory pattern by identifying its instance as a specific type.
Cons: Each method is to be re-created in each instance.

function Person(name,age,job){    this.name = name;    this.age = age;    this.job = job;    this.sayName = function(){        alert(this.name)    }}var person1 = new Person("gr",12,"Grgr");var person2 = new Person("gr44",45,"4y4");
Prototype mode

Each function has a prototype prototype attribute, which is a pointer to an object that is intended to contain properties and methods that can be shared by all instances of a particular type.

Pros: Let all object instances share the properties and methods that it contains. Add this information directly to the prototype object.
function Person(){}Person.prototype.name = "tom";Person.prototype.age = 29;Person.prototype.jobn = "ghrrh";var person = new Person();
A pit in prototype mode
function Person(){}Person.prototype.name = "tom";Person.prototype.age = 29;Person.prototype.job = "ghrrh";//有些时候为了图方面会将其写成下面这样Person.prototype = {    name: "tom",    age: 29,    job: "ghrrh"}//但是这时候无意间就重写了默认的prototype对象,因此constructor属性变成了新对象的constructor属性(指向Object构造函数),不再指向Person函数var person = new Person();
Yes, and here's a prototype object to be introduced:

Whenever a new function is created, a prototype property is created based on a specific set of rules, which points to the prototype object. By default, all prototype objects get a constructor property that contains a pointer to the function where the prototype property is located.

Disadvantages:

In fact, very few people use prototype mode alone, because the prototype pattern is to have all object instances share properties and methods on a prototype object.
For a base type, you can define it in an instance and overwrite it in the prototype, but for a reference type, modifying a property of a reference type on one instance (Person1) will cause a change in the reference type of the other instance (Person2), which is not the effect we want. As a result, constructors and prototype patterns are used together.

Combining the constructor pattern with the prototype pattern
function Person(name,age,job){    this.name = name;    this.age = age;    this.job = job;    this.friends = ["tom","tom1"];}Person.prototype = {    constructor: Person,    sayName: function(){        alert(this.name);    }}var person1 = new Person("tom",20,"hhtt");var person2 = new Person("tom1",45,"chengxuyuan");
Dynamic Prototyping Mode

Sometimes it can be confusing to write structures and prototypes like this.
The dynamic prototype pattern solves this problem by encapsulating all the information in the constructor, and by initializing the prototype in the constructor (only if necessary).

function Person(name,age,job){    this.name = name;    this.age = age;    this.job = job;    if(typeof this.sayName != "function"){        Person.prototype.sayName = function(){            alert(this.name);        }    }}
Parasitic constructor mode

It is recommended that you do not use this mode in situations where other modes are available.

Secure constructor Mode

Safe object: As the name implies, is very safe, refers to the absence of public properties, and its methods do not reference this object. Secure objects are best suited in some secure environments where this and new are forbidden.

function Person(naem,age,job){    var o = new Object();    o.sayName = function(){        alert(name);    }}var friend = new Person("gg",20,"ghrhr");friend.sayName(); //gg

Summary: The first is the factory model.
In order to identify a particular type of object, a constructor pattern appears.
In order to reduce the creation of multiple instances that do not have much relationship, waste memory and prototype mode.
In order to reduce the effect of modifying properties on one instance on another instance, the pattern of combining prototype mode and constructor appears.
As for the later dynamic prototype patterns, parasitic constructor patterns, secure constructor patterns, these are used in their own discretion.
Summing up here, to provide you with a memory idea.

The differences and pros and cons of several JavaScript creation functions.

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.