JavaScript內(nèi)部,字符以UTF-16的格式儲(chǔ)存,每個(gè)字符固定為2個(gè)字節(jié)。對(duì)于那些需要4個(gè)字節(jié)儲(chǔ)存的字符(Unicode碼點(diǎn)大于0xFFFF的字符),JavaScript會(huì)認(rèn)為它們是兩個(gè)字符。 ES6新增了完全支持UTF-16的方法codePointAt(),該方法接受編碼單元的位置而非字符位置作為參數(shù),返回與字符串中給定位置對(duì)應(yīng)的碼位,即一個(gè)整數(shù)值
var text = "a" ;
console.log(text.charCodeAt(0)); // 55362
console.log(text.charCodeAt(1)); // 57271
console.log(text.charCodeAt(2)); // 97
console.log(text.codePointAt(0)); // 134071
console.log(text.codePointAt(1)); // 57271
console.log(text.codePointAt(2)); // 97
二、includes1. indexOf用來(lái)查找某個(gè)元素的位置,如果不存在就返回-1,但是不能判斷是否有NaN的元素。 2. Array.includes()函數(shù)判斷是否包含某一元素,返回 true / false,不能定位元素,但是能判斷 NaN。
const arr1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', NaN]
console.log('%s', arr1.indexOf(NaN)) // -1
console.log(arr1.includes('c')) // true console.log(arr1.includes('z')) // false console.log(arr1.includes(NaN)) // true復(fù)制代碼
三、startsWith1. 確定字符串是否以指定字符串的字符開(kāi)頭,返回 true/false。注意:區(qū)分大小寫(xiě)! 2. 接受兩個(gè)參數(shù): 第一個(gè)參數(shù),要在此字符串開(kāi)頭搜索的字符; 第二個(gè)參數(shù)是指定從字符串開(kāi)始的位置,默認(rèn)從零開(kāi)始 四、endsWith1. 從字符串的末尾開(kāi)始查找 五、repeat1. 返回一個(gè)新字符串,表示將原字符串重復(fù)n次
let str1='a'; let str2=str1.repeat(3);
console.log(str2)//aaa復(fù)制代碼
六、String.fromCodePoint七、copyWithin1. 用于操作當(dāng)前數(shù)組自身,用來(lái)把某些位置的元素復(fù)制并覆蓋到其他位置上去。 2. 該函數(shù)有三個(gè)參數(shù): target:目的起始位置; start:復(fù)制源的起始位置,可以省略,可以是負(fù)數(shù); end:復(fù)制源的結(jié)束位置,可以省略,可以是負(fù)數(shù),實(shí)際結(jié)束位置是end-1。 3.
const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
arr1.copyWithin(1, 3, 6)
console.log('%s', JSON.stringify(arr1)) // [1,4,5,6,5,6,7,8,9,10,11] 復(fù)制代碼
目標(biāo)的位置不夠的,能覆蓋多少就覆蓋多少
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
arr2.copyWithin(3)
console.log('%s', JSON.stringify(arr2)) // [1,2,3,1,2,3,4,5,6,7,8]復(fù)制代碼
start和end都可以是負(fù)數(shù),負(fù)數(shù)表示從右邊數(shù)過(guò)來(lái)第幾個(gè)
const arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
arr3.copyWithin(3, -3, -2)
console.log(JSON.stringify(arr3)) // [1,2,3,9,5,6,7,8,9,10,11]復(fù)制代碼
八、find1. 查找目標(biāo)元素,找到就返回該元素,找不到返回undefined
const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
var ret1 = arr1.find((value, index, arr) => { return value > 4
})
var ret2 = arr1.find((value, index, arr) => { return value > 14
})
console.log('%s', ret1) // 5
console.log('%s', ret2) // undefined復(fù)制代碼
九、findIndex1. 查找目標(biāo)元素,找到就返回元素的位置,找不到就返回-1
var ret3 = arr1.findIndex((value, index, arr) => { return value > 4
})
var ret4 = arr1.findIndex((value, index, arr) => { return value > 14
})
console.log('%s', ret3) // 4
console.log('%s', ret4) // -1復(fù)制代碼
十、fill1. 使用制定的元素填充數(shù)組 2. 參數(shù): value:填充值。 start:填充起始位置,可以省略。 end:填充結(jié)束位置,可以省略,實(shí)際結(jié)束位置是end-1。
const arr1 = [1, 2, 3, 4, 5]
arr1.fill(7)
console.log(arr1) // [7, 7, 7, 7, 7]復(fù)制代碼
十一、entries(),keys()和values() —— 用于遍歷數(shù)組1. 區(qū)別是keys()是對(duì)鍵名的遍歷、values()是對(duì)鍵值的遍歷,entries()是對(duì)鍵值對(duì)的遍歷
for (let index of ['a', 'b'].keys()) {
console.log(index) // 0 1
}復(fù)制代碼
十二、Array.from1. 將對(duì)象轉(zhuǎn)換成數(shù)組 2. 條件: 1)部署了Iterator接口的對(duì)象,比如:Set,Map,Array 2)類(lèi)數(shù)組對(duì)象,就是一個(gè)對(duì)象必須有l(wèi)ength屬性,沒(méi)有l(wèi)ength,轉(zhuǎn)出來(lái)的就是空數(shù)組。 轉(zhuǎn)換map 
轉(zhuǎn)換set 
轉(zhuǎn)換字符串
Array.from('hello world') // ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]復(fù)制代碼
Array.from('\u767d\u8272\u7684\u6d77') // ["白", "色", "的", "海"]復(fù)制代碼
類(lèi)數(shù)組對(duì)象
Array.from({
0: '0',
1: '1',
3: '3',
length:4
})
// ["0", "1", undefined, "3"]復(fù)制代碼
Array.from({
0: 0,
1: 1
})
// []復(fù)制代碼
3. 參數(shù): 1)被轉(zhuǎn)換的的對(duì)象。 2)map函數(shù)。 3)map函數(shù)中this指向的對(duì)象。
let diObj = {
handle: function(n){ return n + 2
}
}
Array.from(
[1, 2, 3, 4, 5], function (x){ return this.handle(x)
},
diObj
) // [3, 4, 5, 6, 7]復(fù)制代碼
十三、Array.of1. new Array()構(gòu)造數(shù)組的時(shí)候,是有二意性的 構(gòu)造時(shí),傳一個(gè)參數(shù),表示生成多大的數(shù)組。 構(gòu)造時(shí),傳多個(gè)參數(shù),每個(gè)參數(shù)都是數(shù)組的一個(gè)元素。 2. 將一個(gè)或多個(gè)值轉(zhuǎn)換成數(shù)組 === new Array() 傳多個(gè)參數(shù) 的情況
|