Three methods for creating javascript objects this article will share with you various methods for creating objects in js and their advantages and disadvantages. The specific content is as follows:
The first method is to create an object:
Create a JSON object
Recommended scenarios: use only once as a function parameter. For example, set a function prototype object.
var object = { name: "Eric", age: 24, sayHi: function(){ console.log("My name is "+ this.name + " this year is " + this.age + "years old.") }};
The second method is to create an object:
Create an Object
var object = new Object();object.name = "Eric";object.age = 24;object.sayHi = function(){....};
The disadvantage of the above two methods of object creation: it cannot be used as a template for object creation, that is, it cannot be used as a new object to construct a new object.
The third method is to create an object:
function Person() { this.name = "Eric"; this.age = 24; this.sayHi = function(){ console.log(this.name); }} var object1 = new Person();var object2 = new Person();
This method solves the disadvantages of the first two methods. It can be used as a template for object creation and can be reused to create multiple objects.
Functions of the new operator:
Execute the constructor (the function behind new) and create an empty object in the constructor.
Associate the empty object created in the previous step with the prototype object of the constructor.
Then point this to the current empty object.
After the constructor is executed, if no return is returned, the empty object is returned to the object.
New operator Principle
The third method has a disadvantage: the internal functions of the object will save a copy in each object. If there are many created objects, it will be a waste of memory. The behavior of a function is that all objects can be shared without the need to save a copy of each object. Therefore, you can place the function in the prototype for declaration, so all objects have public functions, and only one copy of the memory is retained. All attributes are written to the internal object.
The third method is beta1:
function Person() { this.name = 'Eric'; this.age = 24;}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);
Problem: 1. If the order of parameters transmitted by the caller changes, the call is canceled.
Problem: 2. Increase or decrease of parameters will lead to changes in the function declaration, and the call location may also change.
Solution: continue to upgrade beta3:
Function Person (option) {// use an object to overwrite all parameters 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 optimization and put the initialization 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();