Custom Function constructor rating not rated
SoftXML
                  Programming Books
                Open Web Directory
           SoftXMLLib | SoftEcartJS
xml products   Home page    contact    site map
Contact Us
Latest Blog Articles:   Latest SEO Articles | RSS Feed
DOM manipulation best practices
JavaScript Module Pattern
How to start developing with the Nextjs
Angular application structure
Write Guest Articles
Articles Archive




Loading...

The Art & Science of JavaScript ($29 Value FREE For a Limited Time)
The Art & Science of JavaScript ($29 Value FREE For a Limited Time)









Custom Function constructor


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.

Tag cloud

()

(name)

+ //

Function

Person added

behind

constructor

constructors

created empty

example

function

happens

implementation

implicitly

inherits

inside

language

memory

method methods object

object  

objects

operation

operator

opportunity

properties prototype referenced

returned

returns

say()

similar

that’s

Rate This Article
(votes 2)

No Comments comments

Post Comment

We love comments on this blog - they are as important as anything we write ourself. They add to the knowledge and community that we have here. If you want to comment then you�re more than welcome � whether you feel you are a beginner or an expert � feel free to have you say.



* = required
Leave a Reply
Name *:
Email *
(will not be published):
Website:
Comment *:
Human Intelligence Identification *:
What is the background color of this web page?
  
Close
Please enter a valid email Please enter a valid name Please enter valid email Please enter valid name Enter valid year
™SoftXML.   Privacy Statement  |  Article Archive  |  Popular Web Development Books
^Top