Comparison of advantages and disadvantages of js object creation methods, and advantages and disadvantages of js object Creation

Source: Internet
Author: User

Comparison of advantages and disadvantages of js object creation methods, and advantages and disadvantages of js object Creation

Several Methods for comparing object creation in js

1. Factory Model

function createObj(name, sex){    var obj = new Object();    obj.name = name;    obj.sex = sex;    obj.sayName = function(){      alert(this.name);    }    return obj;  }var person = createObj('Tom', 'man');

Disadvantages: ① The Object type cannot be determined (because all objects ).

② The created objects are not associated.

2. Constructor

function createObj(name, sex){    this.name = name;    this.sex = sex;    this.sayName = function(){      alert(this.name);    }  }  var person = new createObj('Tom', 'man');

Disadvantages: ① multiple instances are created repeatedly and cannot be shared.

② Multiple instances have the sayName method, but they are not instances of the same Function.

3. Prototype Method

function createObj(){}  createObj.prototype.name = 'Tom';  createObj.prototype.sex = 'man';  createObj.prototype.sayName = function(){    alert(this.name);  }var person = new createObj();

Disadvantages: ① parameters cannot be passed in, and attribute values cannot be initialized.

② If the value of one instance is changed when the value of the reference type is included, it will be reflected in all instances.

4. Recommended combination (constructor + prototype)

function createObj(name, sex){  this.name = name;  this.sex = sex; } createObj.prototype.sayName = function(){  alert(this.name); } var person = new createObj('Tom', 'man');

Advantage: constructor shares instance attributes, prototype sharing methods, and attributes to be shared. You can pass parameters to initialize attribute values.

5. Dynamic Prototype Method

function createObj(name, sex){  this.name = name;  this.sex = sex;  if(typeof this.sayName != 'function'){   createObj.prototype.sayName = function(){    alert(this.name);   }  } } var person = new createObj('Tom', 'man');

NOTE: if statements can only be called once, that is, they are executed when the method of the first instance is called. This method will be shared by all instances thereafter. In the dynamic prototype method, the prototype cannot be rewritten using the object literal.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.