Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Constructor Function Example in JavaScript

DZone's Guide to

Constructor Function Example in JavaScript

Check out Ted Neward's explanation and example of a Constructor Function in JavaScript.

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

A Constructor Function implementation in JavaScript.

(Comments and constructive criticism desired.)

Implementation: JavaScript

JavaScript provides a pretty reasonable implementation of a Constructor Function; as a matter of fact, most of the time "modern JavaScript" (as described by Crockford prefers the Constructor Function, or some variant of it, over the more traditional object-oriented approach.

Constructor Functions are often used in conjunction with Function Objects and/or Closure-based State, but can easily be extended to a more traditional object-oriented environment.

Within JavaScript, we have essentially a combination of both of those worlds; we have a highly dynamic environment, like LISP, but it’s got objects "built-in", except without some of the more traditional object features, and so on. As one colleague once put it, "You’ve got just enough of both worlds to make you think it’s the best of each, and not enough of the worst of both worlds to make you give it up entirely."

Thus, if the goal is to create a Product instance whose state is properly protected away from prying eyes (namely, the client who receives the Product instance back from the Constructor Function), we can do this by making use of Closure-based State, like so:

var creator = function(somestate) {
  var state = somestate;
  return function() {
    var product = {};
    product.Operation = function() {
      console.log("Doing Operation on " + state);
    };
    return product;
  }();
};

var product1 = creator(27);
product1.Operation();
var product2 = creator(48);
product2.Operation();

The client is oblivious to the actual work necessary to create the object; the only interface surfaced to the client is the Constructor Function, and how it works is entirely irrelevant/encapsulated.

However, a Constructor Function needs to be able to allow for variance in the objects returned without the client being aware of the actual work necessary to do so; in some cases, this may require ConstructorFunctions to make use of one another as a sort of "stepping stone" to the finished OtherProduct:

var anotherCreator = function(somestate, someotherstate) {
  var other = someotherstate;
  return function() {
    var anotherProduct = creator(somestate);
    anotherProduct.OtherOp = function() {
      console.log("Doing OtherOperation on " + other);
    };
    return anotherProduct;
  }();
};

var product3 = anotherCreator(5, 22);
product3.Operation();
product3.OtherOp();

Obviously, nothing stops the second Creator from simply building the AnotherProduct to look identical to the Product and returning that, but this way any changes to the original Product object’s surface area will be preserved in the returned AnotherProduct, without required change to AnotherCreator.

Note that "creator" and "anotherCreator" can both exist, or one can silently supplant the other, such as:

// First creator
var creator = function(somestate) {
  var state = somestate;
  return function() {
    var product = {};
    product.Operation = function() {
      console.log("Doing Operation on " + state);
    };
    return product;
  }();
};

// Second creator
var origCreator = creator;
var creator = function(somestate, someotherstate) {
  var other = someotherstate;
  return function() {
    var anotherProduct = origCreator(somestate);
    anotherProduct.OtherOp = function() {
      console.log("Doing OtherOperation on " + other);
    };
    return anotherProduct;
  }();
};

var product4 = creator(55, 55);
product4.Operation();

Care must be taken to avoid infinite recursion; the original creator must be preserved and referenced through some other name, otherwise calling "creator" from within "creator" will unintentionally recurse.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
java ,javascript ,architecture ,design patterns

Published at DZone with permission of Ted Neward, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}