In addition to using literals and built-in Function constructors, objects can be created using your own Function constructors, as demonstrated in the following example:
var greg = new Person(“Greg”);
greg.say(); // “I am Greg”
This new template is very similar to creating objects in the Java language using the Person class. The syntax is very similar, but in reality there are no classes in the JavaScript language, and Person is just a function.
The following is one possible implementation of the Person constructor function.
var Person = function (name) {
this.name = name;
this.say = function () {
return “I am “ + this.name;
};
};
When calling the constructor function with the new operator, the following happens inside the function:
- An empty object is created that inherits the properties and methods of the function prototype, and a reference to it is stored in the this variable.
- Adding new properties and methods to an object is done using the this link.
- At the end, the function implicitly returns the object referenced this variable (if no other object is explicitly returned).
Here`s what happens behind the scenes:
var Person = function (name) {
// create an empty object
// using a literal
// var this = {};
// properties and methods are added
this.name = name;
this.say = function () {
return “I am“ + this.name;
};
// return this;
};
For simplicity, the say() method has been added to the this link. As a result, with each call to the new Person() constructor, a new function will be created in memory. Obviously, this is an uneconomical approach to memory consumption, because the implementation of the say() method does not change from one instance to another. It would be more efficient to add a method to the prototype of the Person function.
Person.prototype.say = function () {
return “I am “ + this.name;
};
Remember that common members of all instances, such as methods should be added to the prototype.
There is one more thing worth mentioning for completeness. It was said above that inside the constructor, behind the scenes, the following operation is performed:
// var this = {};
This is not entirely true, because an “empty” object is not reallyis empty - it inherits properties and methods from the prototype of the Person function. That is, it would be more accurate to present this operation as follows:
// var this = Object.create(Person.prototype);
Values returned by constructors
When called with the new operator, the constructor function always returns an object. By default, this is the object referenced by this. If no properties are added to the constructor inside it, an "empty" object is returned ("empty", except for the properties and methods inherited from the prototype of the constructor).
Constructors implicitly return this, even if they do not have a return statement. However, the programmer retains the opportunity to return any other object of his choice. The following example creates and returns a new object referenced by the that variable.
var Objectmaker = function() {
// this `name` property will be ignored,
// because the constructor
// returns a completely different object
this.name = “This is it”;
// create and return a new object
var that = {};
that.name = “And that’s that”;
return that;
};
// check
var o = new Objectmaker();
console.log (o.name); // “And that’s that”
As you can see, there is always the opportunity to return any object from the constructor, provided that it is really an object. An attempt to return something else that is not an object (for example, a string or a boolean false) will not be considered an error, but it will be ignored and the constructor will return the object pointed to this link.