lodash里的toLength和toPairs方法詳解
正文
本篇章我們將認(rèn)識(shí)lodash里的toLength方法和toPairs方法實(shí)現(xiàn),同時(shí)在實(shí)現(xiàn)toPairs方法的過程中也能了解到其他封裝的內(nèi)部方法的實(shí)現(xiàn)。
toLength
toLength方法主要是將參數(shù)value轉(zhuǎn)換為用作類數(shù)組對(duì)象的長度整數(shù)。
使用如下:
toLength(3.2)
// => 3
toLength(Number.MIN_VALUE)
// => 0
toLength(Infinity)
// => 4294967295
toLength('3.2')
// => 3
在實(shí)現(xiàn)上,toLength方法借助內(nèi)部封裝導(dǎo)出的toInteger方法,在 《 lodash里to系列之如何將數(shù)據(jù)轉(zhuǎn)換成數(shù)字類型 》中,我們已經(jīng)了解了toInteger方法,意在將參數(shù)轉(zhuǎn)換為整數(shù)。
實(shí)現(xiàn)上對(duì)于不存在的參數(shù)直接返回0,其次將參數(shù)轉(zhuǎn)換為整數(shù),小于0的返回0,大于最大數(shù)MAX_ARRAY_LENGTH返回最大數(shù),該方法返回的整數(shù)范圍為[0,4294967295]。
源碼如下:
import toInteger from './toInteger.js'
const MAX_ARRAY_LENGTH = 4294967295
function toLength(value) {
if (!value) {
return 0
}
value = toInteger(value)
if (value < 0) {
return 0
}
if (value > MAX_ARRAY_LENGTH) {
return MAX_ARRAY_LENGTH
}
return value
}
toPairs
toPairs方法主要是創(chuàng)建一個(gè)object對(duì)象自身可枚舉屬性的鍵值對(duì)數(shù)組。這個(gè)數(shù)組可以通過_.fromPairs撤回。如果object 是 map 或 set,將會(huì)返回其條目。
使用如下:
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
_.toPairs(new Foo);
// => [['a', 1], ['b', 2]]
toPairs方法在實(shí)現(xiàn)上借助內(nèi)部封裝的createToPairs工廠函數(shù)和keys方法實(shí)現(xiàn),其中keys是對(duì)外導(dǎo)出的方法。
源碼如下:
import createToPairs from './_createToPairs.js'; import keys from './keys.js'; var toPairs = createToPairs(keys);
createToPairs
createToPairs方法是內(nèi)部方法,充當(dāng)對(duì)象pairs過程的工廠函數(shù),實(shí)現(xiàn)上,先通過getTag獲取對(duì)象的數(shù)據(jù)類型標(biāo)簽,對(duì)于Map對(duì)象返回mapToArray調(diào)用結(jié)果,對(duì)于Set對(duì)象返回setToPairs調(diào)用結(jié)果,其他對(duì)象類型直接返回baseToPairs調(diào)用結(jié)果。
import baseToPairs from './_baseToPairs.js';
import getTag from './_getTag.js';
import mapToArray from './_mapToArray.js';
import setToPairs from './_setToPairs.js';
var mapTag = '[object Map]',
setTag = '[object Set]';
function createToPairs(keysFunc) {
return function(object) {
var tag = getTag(object);
if (tag == mapTag) {
return mapToArray(object);
}
if (tag == setTag) {
return setToPairs(object);
}
return baseToPairs(object, keysFunc(object));
};
}
baseToParis
baseToParis作為內(nèi)部方法,主要處理普通對(duì)象的pairs過程,通過arrayMap方法處理參數(shù)。
import arrayMap from './_arrayMap.js';
function baseToPairs(object, props) {
return arrayMap(props, function(key) {
return [key, object[key]];
});
}
arrayMap
arrayMap作為內(nèi)部方法,主要處理數(shù)組的映射。
function arrayMap(array, iteratee) {
var index = -1,
length = array == null ? 0 : array.length,
result = Array(length);
while (++index < length) {
result[index] = iteratee(array[index], index, array);
}
return result;
}
mapToArray
mapToArray作為內(nèi)部方法,主要處理Map對(duì)象轉(zhuǎn)換數(shù)組,實(shí)現(xiàn)上通過遍歷獲取。
function mapToArray(map) {
var index = -1,
result = Array(map.size);
map.forEach(function(value, key) {
result[++index] = [key, value];
});
return result;
}
setToPairs
setToPairs作為內(nèi)部方法,主要處理Set對(duì)象轉(zhuǎn)換數(shù)組,實(shí)現(xiàn)上通過遍歷獲取。
function setToPairs(set) {
var index = -1,
result = Array(set.size);
set.forEach(function(value) {
result[++index] = [value, value];
});
return result;
}
小結(jié)
本篇章我們通過了解toLength方法和toPairs方法的實(shí)現(xiàn),同時(shí)也認(rèn)識(shí)了如處理map和set向數(shù)組的轉(zhuǎn)化,以及普通對(duì)象轉(zhuǎn)換數(shù)組的baseToPairs方法,更多關(guān)于lodash方法toLength toPairs的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序(三):網(wǎng)絡(luò)請(qǐng)求
本篇文章主要介紹了微信小程序(三):網(wǎng)絡(luò)請(qǐng)求。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Css-In-Js實(shí)現(xiàn)classNames庫源碼解讀
這篇文章主要為大家介紹了Css-In-Js實(shí)現(xiàn)classNames庫源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
前端canvas中物體邊框和控制點(diǎn)的實(shí)現(xiàn)示例
這篇文章主要為大家介紹了前端canvas中物體邊框和控制點(diǎn)的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
用Move.js配合創(chuàng)建CSS3動(dòng)畫的入門指引
這篇文章主要介紹了用Move.js配合創(chuàng)建CSS3動(dòng)畫的入門指引,文中介紹了這個(gè)JavaScript庫中的一些基本方法的使用,需要的朋友可以參考下2015-07-07
微信小程序 wx.uploadFile在安卓手機(jī)上面the same task is working問題解決
這篇文章主要介紹了微信小程序 wx.uploadFile在安卓手機(jī)上面the same task is working問題解決的相關(guān)資料,需要的朋友可以參考下2016-12-12

