You can n ow construc t a Vector object either by explicitly providing the initial values
for both components or omitting initial values altogether:
var vec1 = new Vector(4, -15); //Sets x to four and y to -15
var vec2 = new Vector(); //Sets x and y to zero
11.5 Factory Methods
Professor: Constructor overloading may be an elegant solution for many needs but
there is a problem when you want to write two dierent constructors that both happen
to have the same number of necessary arguments. Co nsider, for example, that you
want to construct a vector using polar instead of Cartesian coo rdinates as arguments.
Namely, instead of giving x- and y-coordinates denoting an end point of a vector in a
Cartesian coordinate system, you can define the same end point in a polar coordinate
system by specifying its distance (d) from the origin and the angle (α) made between
the vector and the positive x-axis. The polar coordinates d a nd α can be converted to
the Cartesian coordinates x and y by means of the trigono metric f unctions sine and
cosine. The next drawing shows the relation between both coordinate systems.
x
y
d
y = d sin(α)
x = d cos(α)
α
Note that a vector in the polar coo rdinates is also specified by two values just as it is
in the Cartesian coordina te s and it is impossible to write a single constructor that will
know which of the two coordinate systems you’ve had in mind when you pass two
arguments. JavaScript allows you to have two constructors with dierent names but
that is not a recommended technique. An entirely b etter approach is to use a factory
method.
A factory method is an ordinary class method, only that it initiates and returns an
instance object o f the class. For example, the next definition makes polar() a factory
method of the Vector class that creates a Vector object instance initialized with pola r
coordinates:
Vector.polar = function(d, alpha) {
//Converts degrees to radians.
var rads = Math.PI * alpha / 180;
//Converts polar coordinates to Cartesian and calls the original
//constructor.
return new Vector(d * Math.cos(rads), d * Math.sin(rads));
};
212 Meeting 11. Building Your Own Objects
Recall th at trigonometric functions of the Math object use radians rather than degrees.
If we want our factory function to accept degrees, then the conversion perfor med in
the first line of the fu nction body is necessary. The last line of the metho d converts
polar coordinates to Cartesian and passes the latter to the original Vector() construc-
tor. The object cre ated by the constructor is then returned fro m the pol ar() factory
method. Note that because we’re just adding a new pr operty to the existing Vector
class, we don’t use the var keyword before the polar() method definition.
Maria: How on Ear th can we invoke this factory method? We’re only just creating
a vector obje ct instance with it and ther e exists as yet no ob je ct on which to invoke
the method. Or must we c onstruct o ne in o rder to be able to use the above factory
method?
Professor: I see what you mean. Recall that everything you see in JavaScript is an
object. Whe n w e defined th e Vector() constructor, an object was created for that
definition and a r eference to the object was assigned to Vector. Then we appended
the polar() method to the existing Vector class object. As a consequence, we can
invoke polar() on the Vector class just as if the class was an object instance:
var v3 = Vector.polar(2, 90);
console.log(v3.x); //Writes 1.2246467991473532e-16
console.log(v3.y); //Writes 2
Mike: Is the above polar() method like the static methods now() and UTC() of the
Date class?
Professor: Exactly. You can call a static method on a class object but there are lim-
itations on what such a method can do. Note that there’s no object connected with a
static method other than the class definition obje ct. What’s more, this inside a static
method r efers to the static metho d itself a nd is therefore rare ly of any use. That means
that a static method c annot d irectly access any properties of object instances of the
class that it belongs to.
Mike: Wait a minute! How then can a constructor store property values?
Professor: A construc tor is diere nt because it actually creates an object instance,
something that a factory method cannot do. Notice that a factory method still needs
to call a constructor in order to create an object instance. Furtherm ore, you shall not
confuse a constructor definition object with an objec t instance that is created as a result
of invoking the constructo r. The this keyword used in sid e a constructor refers to the
object instance that is just being created by the co nstructor. That mea ns that property
values are stored in the newly created object instance and n ot in th e cla ss object itself.
Maria: By the way, I th ink that the x component in the above example should be
exactly zero and not just a very small number. Is that due to the lack of precision?
Professor: That’s true. Math.PI is, as are all numbers in JavaScript, limited to the
precision of 15 significant digits. Because of th at, an angle of 90 degrees is not con-
verted exactly to π/ 2, which means that the cosine of the angle isn’t precisely zero
as well. In cases like ours such errors are not dicult to c ontrol and usually a simple
11.5. Factory Methods 213
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.7.205