October 2006
Beginner to intermediate
352 pages
9h 33m
English
Use the Numberâs prototype property:
Number.prototype.triple = function (ââ) {
var nm = this.valueOf(ââ) * 3;
return nm;
}
var num = new Number(3.0);
alert(num.triple(ââ));Declare the data member with var instead of this. The purpose behind data hiding is to control how the data is accessed or updated.
Use the throw statement to trigger an error. Then implement try...catch in the calling application:
if (typeof value != "number") {
throw "NotANumber";
}Unlike the event object, there are more than just model differences involved. Not only is the property different, but so is the value thatâs assigned to the property.
Hereâs one approach to creating the objects:
function Control(ââ) {
var state = 'on';
var background = '#fff';
this.changeState = function(ââ) {
if (state == 'on') {
state = 'off';
background = '#000';
} else
state = 'on';
background = '#fff';
};
this.getState = function(ââ) {
return state;
};
this.getColor = function(ââ) {
return background;
};
}Read now
Unlock full access