Professor: This isn’t a problem in this particular c ase. Because the max() method is
being defined as an instance metho d of the Array class, the method is never accessible
without an objec t reference. Hence, if you want to access the ma x() method from
within the same me thod, you sho uld use the expression th is.max().
Maria: I see. Can we test our ma x() method to see how it works?
Professor: No problem . Here are some examples:
var a1 = [7, 3, 8, -1, 99, 5];
var a2 = [];
var a3 = [5, "w", 66, 2];
var a4 = [-3, -12, ""];
console.log(a1.max()); //Writes 99
console.log(a2.max()); //Writes -Infinity
console.log(a3.max()); //Writes NaN
console.log(a4.max()); //Writes 0
Before we continue, I should mentio n that it is not always possible to add methods
in this way to classes implemented by dier ent host environme nts, like browsers, for
example. These are classes th at are not defined by th e core JavaScript language and
are characterized as part o f client-side JavaScript in the JavaScript reference at the end
of the book.
11.7 More on Setting and Querying Object Properties
Professor: One of the key concepts in the object-or ie nted programmin g paradig m is
encapsulation, which, formally, bears more than a single meaning. One definition of
encapsulation is that it is a mechanism for information hiding. In practice this means
that the intern al representation of a n object is separated and hidde n from a programmer
who uses the object. Take, for example, our Vector class. The class is in te rnally
represented by the Cartesian coordinates x and y, but that need not always be the case.
You could just as well decide to store vectors in polar form. In order to hide that detail
from the programmer, you can provide special meth ods that allow the programme r
to access the object data indirectly. If the object’s internal data representation ever
changes, then you simply reprogram the access methods to perform necessary data
transform ations so tha t they can be used in exactly the same way as they had been
before the change.
For example, if you want to w rite the value of the x component of the v4 vector from
the example on page 214 to the JavaScript Console, you currently have no other op tion
but to access the component directly:
console.log(v4.x);
That is not such a good idea, tho ugh, especially when direct access like this one is
sprinkled all over the co de. Consider, for example, that you write, or even g et from
another autho r the new version of the Ve ctor class, w hose internal repre sentation of
11.7. More on Setti ng and Querying Object Properties 217
vectors has been changed from Cartesian to polar. There’s a lot of work awaiting you
in bringing the whole program up to date with the change.
Instead of dir ectly accessing prop erties, you can write methods to set and query the
object properties. By the way, JavaScript has a special mechanism that allows you to
write so-called getters and setters to set and query th e objec t properties. However, I
personally don’t like to use it because it is not immediately evident from the property
access expr ession th at in fact a method is called behind the scenes.
Anyway, you can still define yo ur own methods to set and query the x property of
Vector. This is an exam ple:
Vector.prototype.setX = function(x) {
this.x = x;
};
Vector.prototype.getX = function() {
return this.x;
};
The x property of the v4 vector can now be manipulated like th is:
v4.setX(42);
console.log(v4.getX()); //Writes 42
Now, if the internal representation of the Vector class c hanges, you only need to
rewrite the getX() and setX() methods, while all the code using these methods stays
perfectly OK. For example, this is how the two methods might look if the internal
Vector rep resentation should change to polar:
Vector.prototype.getX = function() {
return this.d * Math.cos(this.alpha);
};
Vector.prototype.setX = function(x) {
var y = this.getY();
this.d = Math.sqrt(x * x + y * y);
this.alpha = Math.atan2(y, x);
};
Notice that in order to be able to compute the values of both polar co ordinates (d and
alpha) from the given x-coordinate, you also need the getY() method. So here are
the getY() and setY() methods as well:
Vector.prototype.getY = function() {
return this.d * Math.sin(this.alpha);
};
218 Meeting 11. Building Your Own Objects
..................Content has been hidden....................

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