Prototype源碼淺析 Number部分
更新時間:2012年01月16日 00:22:55 作者:
Prototype在原生對象的基礎(chǔ)上擴展,分別是Object,F(xiàn)unction,String,Number,Array,Date,前面分析了Object,F(xiàn)unction,String,還剩下Number,Array,Date
Number部分方法比較少,一共有8個:
toColorPart: 將 Number 對象轉(zhuǎn)換為具有兩位數(shù)字的十六進制形式
succ: 返回當前 Number 對象的下一個值,即當前值加一
times: 采用 Ruby 的風格來封裝一個標準的 [0...n] 循環(huán)
toPaddedString:將當前 Number 對象轉(zhuǎn)換為字符串,如果轉(zhuǎn)換后的字符串長度小于 length 指定的值,則用 0 在左邊補足其余的位數(shù)
abs: 返回當前 Number 對象的絕對值。
round: 返回當前 Number 對象四舍五入后的整數(shù)值。
ceil: 返回大于等于當前 Number 對象的最小整數(shù)值。
floor: 返回小于等于當前 Number 對象的最大整數(shù)值。
其中一個重要的方法是toPaddedString。Number對象重寫了toString方法:
NumberObject.toString(radix)
function toPaddedString(length,radix){
var string = this.toString(radix || 10);//先將數(shù)字轉(zhuǎn)換成相應的進制
return '0'.times(length - string.length) + string;//times方法在String中擴展的,將一個字符重復n遍
}
有了這個方法,就有一個比較有用的延伸就是toColorPart,可用于CSS中的顏色轉(zhuǎn)換:
function toColorPart() {
return this.toPaddedString(2, 16);
}
既然是CSS顏色轉(zhuǎn)換,因此數(shù)字就要求在[0-255]范圍內(nèi)。
console.log((10).toColorPart());//0a
有一個和String中同名的方法succ,作用也差不多,String中是按照字符表來遞加的,Number中是按照自然數(shù)的順序來的。
function succ() {
return this + 1;
}
console.log((10).succ());//11
從這個方法出發(fā),來一個簡單的0-n的數(shù)組
function range(){
var ret = [0];
for(var i = 0; i < this - 1; i++){
ret.push(i.succ());
}
return ret;
}
console.log((10).range());//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
暫時用這個range函數(shù)來得到times函數(shù):
function times(iterator, context){
this.range().forEach(iterator, context);//源碼中使用的是R()方法
return this;
}
var s = '';
(5).times(function(item){
s += item;
});
console.log(s);//01234
除去上面幾個方法,其他的方法就是將Math的靜態(tài)方法擴展到Number對象上【說法不準確,意會··=。=】
function abs() {
return Math.abs(this);
}
function round() {
return Math.round(this);
}
function ceil() {
return Math.ceil(this);
}
function floor() {
return Math.floor(this);
}
toColorPart: 將 Number 對象轉(zhuǎn)換為具有兩位數(shù)字的十六進制形式
succ: 返回當前 Number 對象的下一個值,即當前值加一
times: 采用 Ruby 的風格來封裝一個標準的 [0...n] 循環(huán)
toPaddedString:將當前 Number 對象轉(zhuǎn)換為字符串,如果轉(zhuǎn)換后的字符串長度小于 length 指定的值,則用 0 在左邊補足其余的位數(shù)
abs: 返回當前 Number 對象的絕對值。
round: 返回當前 Number 對象四舍五入后的整數(shù)值。
ceil: 返回大于等于當前 Number 對象的最小整數(shù)值。
floor: 返回小于等于當前 Number 對象的最大整數(shù)值。
其中一個重要的方法是toPaddedString。Number對象重寫了toString方法:
NumberObject.toString(radix)
| 參數(shù) | 描述 |
|---|---|
| radix | 可選。規(guī)定表示數(shù)字的基數(shù),使 2 ~ 36 之間的整數(shù)。若省略該參數(shù),則使用基數(shù) 10。但是要注意,如果該參數(shù)是 10 以外的其他值,則 ECMAScript 標準允許實現(xiàn)返回任意值。 |
復制代碼 代碼如下:
function toPaddedString(length,radix){
var string = this.toString(radix || 10);//先將數(shù)字轉(zhuǎn)換成相應的進制
return '0'.times(length - string.length) + string;//times方法在String中擴展的,將一個字符重復n遍
}
有了這個方法,就有一個比較有用的延伸就是toColorPart,可用于CSS中的顏色轉(zhuǎn)換:
復制代碼 代碼如下:
function toColorPart() {
return this.toPaddedString(2, 16);
}
既然是CSS顏色轉(zhuǎn)換,因此數(shù)字就要求在[0-255]范圍內(nèi)。
console.log((10).toColorPart());//0a
有一個和String中同名的方法succ,作用也差不多,String中是按照字符表來遞加的,Number中是按照自然數(shù)的順序來的。
復制代碼 代碼如下:
function succ() {
return this + 1;
}
console.log((10).succ());//11
從這個方法出發(fā),來一個簡單的0-n的數(shù)組
復制代碼 代碼如下:
function range(){
var ret = [0];
for(var i = 0; i < this - 1; i++){
ret.push(i.succ());
}
return ret;
}
console.log((10).range());//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
暫時用這個range函數(shù)來得到times函數(shù):
復制代碼 代碼如下:
function times(iterator, context){
this.range().forEach(iterator, context);//源碼中使用的是R()方法
return this;
}
復制代碼 代碼如下:
var s = '';
(5).times(function(item){
s += item;
});
console.log(s);//01234
除去上面幾個方法,其他的方法就是將Math的靜態(tài)方法擴展到Number對象上【說法不準確,意會··=。=】
復制代碼 代碼如下:
function abs() {
return Math.abs(this);
}
function round() {
return Math.round(this);
}
function ceil() {
return Math.ceil(this);
}
function floor() {
return Math.floor(this);
}
相關(guān)文章
prototype 1.5 & scriptaculous 1.6.1 學習筆記
prototype 1.5 & scriptaculous 1.6.1 學習筆記...2006-09-09
Prototype PeriodicalExecuter對象 學習
這個對象就是可以周期性的執(zhí)行某個方法,但是在它內(nèi)部維持了一個狀態(tài),可以防止由于某些原因一次調(diào)用沒執(zhí)行,然后下一次調(diào)用又來了,這樣會造成連續(xù)執(zhí)行兩次方法。上面的第二斷英文就是這個意思。2009-07-07
Prototype 學習 工具函數(shù)學習($A方法)
Prototype 學習 工具函數(shù)學習($A使用方法)2009-07-07
不錯的一篇關(guān)于javascript-prototype繼承
不錯的一篇關(guān)于javascript-prototype繼承...2007-08-08
初學prototype,發(fā)個JS接受URL參數(shù)的代碼
初學prototype,發(fā)個JS接受URL參數(shù)的代碼...2007-02-02

