3 Ways to create objects in JavaScript _javascript tips

Source: Internet
Author: User
Tags function prototype

This article for everyone to share the JS to create the object of a variety of methods to share the pros and cons of a method, the specific contents are as follows

The first way to create an object:

Creating a JSON Object
Recommended occasions: As a function of the parameters, temporary only use once scene. For example, set the function prototype object.

 var object = {
  name: "Eric",
  age:24,
  sayhi:function () {
   console.log ("My name is" + THIS.name +] this yea R is ' + This.age + ' years old. '
  }}
 ;

The second way to create an object:

Create an Object

 var object = new Object ();
 Object.name = "Eric";
 Object.age =;
 Object.sayhi = function () {...};

The disadvantages of the above two ways of creating objects are the templates that cannot be created as objects, that is, the new object cannot be constructed with the.

The third way to create an object:

 function person () {
  this.name = "Eric";
  This.age =;
  This.sayhi = function () {
   console.log (this.name);
  }
 }

 var object1 = new Person ();
 var object2 = new Person ();

This method of creating objects solves the drawbacks of the first two ways, and can be used as a template for object creation and can be reused to create multiple objects.

The role of the new operator:

Executes the constructor (the function after new), creating an empty object inside the constructor
Associating the empty object you created with the prototype object of the constructor
Then point this to the current null object
Returns an empty object to object after the execution of the constructor completes without return

The new operator principle

The third Way has a disadvantage: the internal functions of an object are stored in each object. If you create a lot of objects, it is a waste of memory. The behavior of a function is that all objects can be shared, and no object is saved in one copy. So, you can put a function in a prototype and declare it, so all objects have a common function, and only one copy of the memory remains. All attributes are written to the inside of the object

The Third Way Beta1:

 function person () {
  this.name = ' Eric ';
  This.age = n;
 }
 Person.prototype = {
  sayhi:function () {
  },
 };

 var object1 = new Person ();
 var object2 = new Person ();

Continue to upgrade Beta2:

 function Person (name,age) {
  this.name = name | | "";
  This.age = Age | | "";
 }
 Person.prototype = {
  sayhi:function () {
  },
 };

 var object1 = new Person (name1,age1);
 var object2 = new Person (name2,age2);

question:1. If the caller changes the order of the parameters, it is obsolete
question:2, the parameter increase or decrease will cause the function declaration change, the place which the call may also change.

How to resolve: Continue to upgrade BETA3:

 function person (option) {//Overwrite all parameters with an object
  this.name = Option.name | | "";
  This.age = Option.age | | "";
 }
 Person.prototype = {
  sayhi:function () {
  },
 };

 var object1 = new Person ({
   name: "Eric",
   age:24
  });
 var object2 = new Person ({
   name: "XXX",
   age:xx
  });

Continue to optimize and place the initialized code in the INIT function

Continue to upgrade BETA4:

 function person (option) {
  this._init (option);
 }
 Person.prototype = {
  _init:function (option) {
   this.name = option.name;
   This.age = Option.age;
  },
  sayhi:function () {
   console.log ("HI");
  }
 ;

 var object1 = new Person ({
   name: "Eric";
   Age:24
  });
 Object1.sayhi ();

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.