Search:  
Gentoo Wiki

Factory_paradigm_of_JavaScript

This page is a candidate for deletion 
Reason given: Off topic. Please use a dedicated javascript / programming site.
If you disagree with its deletion, please explain why on its discussion page.
If you intend to fix it, please remove this notice, but do not remove this notice from articles that you have created yourself.
Make sure no other pages link here and check the page's history before deleting.

Because properties of an object can be defined dynamically after its creation, a lot of developers wrote code similar to the following when JavaScript was first introduced:

var oCar = new Object;
oCar.color = “red”;
oCar.doors = 4;
oCar.mpg = 23;
oCar.showColor = function () {
alert(this.color);
};

In this code, an object is created named car. The object is then given several properties: Its color is red, it has four doors, and it gets 23 miles per gallon. The last property is actually a pointer to a function, which means the property is a method. After this code is executed, you can use an object called car. The problem is that you may need to create more than one instance of a car. To solve the problem, developers created factory functions, which create and return an object of a specific type. For example, a function called createCar() could be used to encapsulate the creation of the car object described previously:

function createCar() {
var oTempCar = new Object;
oTempCar.color = “red”;
oTempCar.doors = 4;
oTempCar.mpg = 23;
oTempCar.showColor = function () {
alert(this.color)
};
return oTempCar;
}
var oCar1 = createCar();
var oCar2 = createCar();

Here, all the previous lines of code are contained within the createCar() function, including one extra line, which returns the car (oTempCar) as the function value. When this function is called, it creates a new Object and assigns all the properties necessary to replicate the car object described earlier. Using this method, it is easy to create two (or more) versions of a car object (oCar1 and oCar2) that have the exact same properties. Of course, the createCar() function can also be modified to allow the passing in of default values for the various properties instead of just assigning default values:

function createCar(sColor, iDoors, iMpg) {
var oTempCar = new Object;
oTempCar.color = sColor;
oTempCar.doors = iDoors;
oTempCar.mpg = iMpg;
oTempCar.showColor = function () {
alert(this.color)
};
return oTempCar;
}
var oCar1 = createCar(“red”, 4, 23);
var oCar1 = createCar(“blue”, 3, 25);
oCar1.showColor(); //outputs “red”
oCar2.showColor(); //outputs “blue”

By adding arguments to the createCar() function, it is possible to assign values to the color, doors, and mpg properties of the car object being created. This leaves two objects with the same properties but different values for those properties. You can edit JavaScript code by JavaScript editor. As ECMAScript became more formalized, however, this method of creating objects fell out of favor and is typically frowned upon today. Part of the reason for this was semantic (it doesn’t look as appropriate as using the new operator with a constructor), and part was functional. The functional problem has to do with the creation of object methods using this paradigm. In the previous example, every time the createCar() function is called, a new function is created called showColor(), meaning that every object has its own version of showColor() when, in reality, each object should share the same function. Some got around this problem by defining the object methods outside of the factory functions and then pointing to them:

function showColor() {
alert(this.color);
}
function createCar(sColor, iDoors, iMpg) {
var oTempCar = new Object;
oTempCar.color = sColor;
oTempCar.doors = iDoors;
oTempCar.mpg = iMpg;
oTempCar.showColor = showColor;
return oTempCar;
}
var oCar1 = createCar(“red”, 4, 23);
var oCar2 = createCar(“blue”, 3, 25);
oCar1.showColor(); //outputs “red”
oCar2.showColor(); //outputs “blue”

In this rewritten code, the showColor() function is defined before the createCar() function. Inside createCar(), the object is assigned a pointer to the already existing showColor() function. Functionally, this solves the problem of creating duplicate function objects; but semantically, the function doesn’t look like it is a method of an object. All these problems led to the creation of developer-defined constructors.

Retrieved from "http://www.gentoo-wiki.info/Factory_paradigm_of_JavaScript"

Last modified: Sun, 09 Sep 2007 06:11:00 +0000 Hits: 1,488