How to create an object using javascript

Source: Internet
Author: User
Create an Object instance
 var person = new Object(); person.name = "zhouquan"; person.age = 21; person.sayName = function(){   console.log(this.name); };  person.sayName();//zhouquan
Object literal type
var person = {  name: "zhouquan",  age: 21,  sayName:function(){    console.log(this.name);  }};person.sayName();//zhouquan

The disadvantage of creating an object by using the common object instance method and object literal method is that these two methods are not suitable for creating the same type of object multiple times.

Factory Model
 function createPerson(name, age, job){   var o = new Object();   o.name = name;   o.age = age;   o.job = job;   o.sayName = function(){    console.log(this.name);   };   return o; }  var person1 = createPerson("zhouquan", 22, "student"); person1.sayName();  //zhouquan      var person2 = createPerson("zhouquan2", 23, "Programer"); person2.sayName();  //zhouquan2

The createPerson () function can construct a Person object containing all necessary information based on accepted parameters. This function can be called countless times, and an object containing several attributes and a method is returned each time. Although the factory mode solves the problem of creating multiple similar objects, it does not solve the problem of Object Recognition (that is, how to know the type of an object ).

Constructor Mode
function Person(name, age){  this.name = name;  this.age = age;  this.sayName = function(){    console.log(this.name);  };}var person1 = new Person("zhouquan", 21);person1.sayName();  //zhouquanvar person2 = new Person("zhouquan2", 23);person2.sayName();  //zhouquan2

In this way, creating an object in a factory mode has the following differences:

  • No created object is displayed
  • Attributes and methods are directly assigned to this object.
  • No return Statement

In addition, note the uppercase letter P used by the function name Person. This is to distinguish it from other functions.

Prototype
function Person(){}Person.prototype.name = "zhouquan";Person.prototype.age = 21;Person.prototype.sayName = function(){  console.log(this.name);};var person1 = new Person();person1.sayName();//zhouquanvar person2 = new Person();person2.sayName();//zhouquan

Prototype(Prototype) attribute. This attribute is a pointer to an object. The purpose of this object is to include attributes and methods that can be shared by all instances of a specific type. The benefit of using a prototype object is that all object instances can share its attributes and methods.

However, this method is not easy to write. You need to write Person. prototype before each attribute or method. I can improve it by combining the literal method of the previous object:

function Person(){}Person.prototype={  name : "zhouquan",  age : 21,  sayName : function(){    console.log(this.name);  }};var person1 = new Person();person1.sayName();//zhouquanvar person2 = new Person();person2.sayName();//zhouquan
Constructor mode and prototype mode
function Person(name, age){  this.name = name;  this.age = age;}Person.prototype={  sayName : function(){    console.log(this.name);  }};var person1 = new Person("zhouquan", 21);person1.sayName();//zhouquanvar person2 = new Person("xiaozhou", 21);person2.sayName();//xiaozhou

This is the most common way to create a custom type. The constructor mode is used to define instance attributes, while the prototype mode is used to define methods and shared attributes. In this way, each instance will have its own copy of the Instance attribute, but it also shares this reference to the method, saving the maximum memory. In addition, this combination mode also supports passing parameters to the constructor. It can be said that this is the default mode used to define the reference type.

Dynamic Prototype
function Person(name, age){  this.name = name;  this.age = age;  if(typeof this.sayName != "function"){    Person.prototype.sayName = function(){      console.log(this.name);    };  }}var person1 = new Person("zhouquan", 21);person1.sayName();//zhouquanvar person2 = new Person("xiaozhou", 21);person2.sayName();//xiaozhou

The sayName () method is added to the prototype only when the sayName () method does not exist. The if statement is executed only when the function is called for the first time. The if statement checks any attributes or methods that should exist after initialization. You don't have to use a lot of if Statements to check each attribute or method. You just need to check one. Note that when using the dynamic prototype mode, you cannot use the object literal to overwrite the prototype, because if the prototype is overwritten in the case of and when an instance is created, then, the connection between the existing instance and the new source instance is cut off.

 

 

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.