Section A.10. Chapter 11


A.10. Chapter 11

  1. Use the Numberâ??s prototype property:

  2. Number.prototype.triple = function (â??â??) {    var nm = this.valueOf(â??â??) * 3;    return nm; } var num = new Number(3.0); alert(num.triple(â??â??));

  3. Declare the data member with var instead of this. The purpose behind data hiding is to control how the data is accessed or updated.

  4. Use the tHRow statement to trigger an error. Then implement try...catch in the calling application:

  5. if (typeof value != "number") {   throw "NotANumber"; }

  6. 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.

  7. Hereâ??s one approach to creating the objects:

  8. 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;   }; }




Learning JavaScript
Learning JavaScript, 2nd Edition
ISBN: 0596521871
EAN: 2147483647
Year: 2006
Pages: 151

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net