Module Number
Number <ul><li>Prototype extends native JavaScript numbers in order to provide:  </li></ul><ul><li>1)ObjectRange compatibi...
Number <ul><li>Examples; </li></ul><ul><li>1)  $R ( 1 ,  10 ).each( function ( index ) { </li></ul><ul><li>     // This ge...
Number <ul><li>succ() -> Number </li></ul><ul><li>Returns the successor of the current Number, as defined by current + 1. ...
Number <ul><li>times(iterator) -> Number </li></ul><ul><li>Encapsulates a regular [0..n[ loop, Ruby-style. </li></ul><ul><...
Number <ul><li>toColorPart() -> String </li></ul><ul><li>Produces a 2-digit hexadecimal representation of the number (whic...
Number <ul><li>toPaddedString(length[, radix]) -> String </li></ul><ul><li>Converts the number into a string padded with 0...
Upcoming SlideShare
Loading in …5
×

Number

273 views

Published on

Prototype Javascript

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
273
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Number

  1. 1. Module Number
  2. 2. Number <ul><li>Prototype extends native JavaScript numbers in order to provide: </li></ul><ul><li>1)ObjectRange compatibility, through a succ method. </li></ul><ul><li>2) Ruby-like numerical loops with a times method. </li></ul><ul><li>3) Simple utility methods such as toColorPart. </li></ul>
  3. 3. Number <ul><li>Examples; </li></ul><ul><li>1) $R ( 1 ,  10 ).each( function ( index ) { </li></ul><ul><li>     // This gets invoked with index from 1 to 10, inclusive}); </li></ul><ul><li>  2) ( 5 ).times( function (n) { </li></ul><ul><li>     // This gets invoked with index from 0 to 5, *exclusive* </li></ul><ul><li>     // The parentheses are due to JS syntax, if we did not use a literal, they'd be superfluous }); </li></ul><ul><li>3) 128.toColorPart() </li></ul><ul><li>// -> '70‘ </li></ul><ul><li>4)  '#' + [128, 10, 16].invoke('toColorPart').join('') </li></ul><ul><li>// -> '#800a10' </li></ul>
  4. 4. Number <ul><li>succ() -> Number </li></ul><ul><li>Returns the successor of the current Number, as defined by current + 1. Used to make numbers compatible with ObjectRange .   </li></ul><ul><li>Examples; </li></ul><ul><li>1) ( 5 ).succ() </li></ul><ul><li>// -> 6 </li></ul><ul><li>2) $A($R(1, 5)).join('') </li></ul><ul><li>// -> '12345' </li></ul>
  5. 5. Number <ul><li>times(iterator) -> Number </li></ul><ul><li>Encapsulates a regular [0..n[ loop, Ruby-style. </li></ul><ul><li>The callback function is invoked with a single argument, ranging from 0 to the number, exclusive. </li></ul><ul><li>Example; </li></ul><ul><li>var  s =  '' ; </li></ul><ul><li>( 5 ).times( function (n) { </li></ul><ul><li>   s += n; </li></ul><ul><li>}); </li></ul><ul><li>s // -> '01234' </li></ul>
  6. 6. Number <ul><li>toColorPart() -> String </li></ul><ul><li>Produces a 2-digit hexadecimal representation of the number (which is therefore assumed to be in the [0..255] range). Useful for composing CSS color strings. </li></ul><ul><li>Examples; </li></ul><ul><li>1) 128.toColorPart() </li></ul><ul><li>// -> '70‘ </li></ul><ul><li>2) 10.toColorPart() </li></ul><ul><li>// -> '0a' </li></ul><ul><li>3) #'  + [ 128 ,  10 ,  16 ].invoke('toColorPart').join('') </li></ul><ul><li>// -> '#800a10' </li></ul>
  7. 7. Number <ul><li>toPaddedString(length[, radix]) -> String </li></ul><ul><li>Converts the number into a string padded with 0s so that the string’s length is at least equal to length. Takes an optional radix argument which specifies the base to use for conversion. </li></ul><ul><li>Examples: </li></ul><ul><li>1) ( 13 ).toPaddedString( 4 ); </li></ul><ul><li>// -> '0013‘ </li></ul><ul><li>( 13 ).toPaddedString( 4 ,  16 ) </li></ul><ul><li>// -> '000d' </li></ul>

×