字符串?dāng)U展
ES6加強(qiáng)了對(duì)Unicode的支持,并且擴(kuò)展了字符串對(duì)象。
獲取字符unicode編碼
var s = "??";
var a = s.codePointAt()
//參數(shù)不寫默認(rèn)是第0位,超出長度為undefined
console.log(a) //134071
var s2 = "abcde";
console.log(s2.codePointAt()) //97
console.log(s2.codePointAt(1)) //98
console.log(s2.codePointAt(2)) //99
console.log(s2.codePointAt(10)) //undefined
通過unicode碼點(diǎn)返回對(duì)應(yīng)字符
//es5 ,不能識(shí)別輔助平面的字符
String.fromCharCode()
console.log(String.fromCharCode(97)) //a
//es6 ,可以識(shí)別輔助平面的字符
String.fromCodePoint()
console.log(String.fromCodePoint(0x20BB7)) //??
字符的Unicode表示法
JavaScript允許采用“\uxxxx”形式表示一個(gè)字符旋廷,其中“xxxx”表示字符的碼點(diǎn),"\u0061"http://a,
但是,這種表示法只限于\u0000——\uFFFF之間的字符坝疼。超出這個(gè)范圍的字符共耍,必須用兩個(gè)雙字節(jié)的形式表達(dá)。
//之前
console.log("\uD842\uDFB7") //??
console.log("\u0041\u0042\u0043") //ABC 省略了00游盲?
//es6,只要將碼點(diǎn)放入大括號(hào)误墓,就能正確解讀該字符蛮粮。
console.log("\u{20BB7}") //??
console.log("\u{41}\u{42}\u{43}") //ABC
復(fù)制字符串
將str復(fù)制幾分并返回,num代表復(fù)制的次數(shù)谜慌。
str.repeat(num)
var s = "abced";
console.log(s.repeat(2)) //abcedabced
//新的字符串 不會(huì)影響原有的
查找字符串 str.includes()
參數(shù): 1然想、要查找的字符串 2、起始位置
返回布爾值欣范,表示是否找到了參數(shù)字符串
var str = "abcdef";
console.log("a") // true 不傳第2個(gè)參數(shù)默認(rèn)為0
console.log("g") //false
console.log("a",1) //false
字符串是否在源字符串的頭部 str.startsWith()
參數(shù): 1变泄、要查找的字符串 2、起始位置
返回布爾值
var str = "abcdef";
console.log(str.startsWith("a")) //true
console.log(str.startsWith("b")) //false
console.log(str.startsWith("b",1)) //true
console.log(str.startsWith("e",3)) //true
表示參數(shù)字符串是否在源字符串的尾部 str.endsWith()
參數(shù): 1恼琼、要查找的字符串 2妨蛹、起始位置(針對(duì)的是n個(gè)字符)
返回布爾值
var str = "abcdef";
console.log(s.endsWith("d")) //true
console.log(s.endsWith("d",3)) //false
console.log(s.endsWith("c",3)) //true
二進(jìn)制和八進(jìn)制表示法
ES6提供了二進(jìn)制和八進(jìn)制數(shù)值的新的寫法,分別用前綴0b和0o表示
Math對(duì)象的擴(kuò)展
Math.trunc() 去除小數(shù)部分返回整數(shù)部分
console.log(Math.trunc(1.25)) //1
Math.sign() 判斷一個(gè)數(shù)字是正數(shù)晴竞,負(fù)數(shù)還是零
返回值:
-1 負(fù)數(shù) 蛙卤;0 零 ; 1 :正數(shù)
console.log(Math.sign(-25)) //-1
console.log(Math.sign(0)) //0
console.log(Math.sign(0.01)) //1
Math.hypot(...values) 返回所有參數(shù)的平方和的平方根(勾股定理)
var h = [3,4]; //
console.log(Math.hypot(...h)) //5
console.log(Math.hypot(2,3,4)) //5.385164807134504
數(shù)組擴(kuò)展
Array.from()
用于將類數(shù)組對(duì)象轉(zhuǎn)為真正的數(shù)組
語法:Array.from(arrayLike[, mapFn[, thisArg]])
//不定參
function fn(){
console.log(Array.from(arguments))
}噩死;
fn(1,2,3,4,5)颤难;//[1, 2, 3, 4, 5]
//字符串
console.log(Array.from("javascipt")) //["j", "a", "v", "a", "s", "c", "i", "p", "t"]
Array.of()
方法用于將一組值,轉(zhuǎn)換為數(shù)組已维。返回
console.log(Arry.of("a","b","c")) //["a","b","c"]
arr.find()
arr.find(callback[, thisArg])
callback接收3個(gè)參數(shù):elment 當(dāng)前遍歷到的元素行嗤, index 當(dāng)前遍歷到的索引,array 數(shù)組本身
找出第一個(gè)符合條件的數(shù)組元素,沒有返回undefined
var arry = [1,25,84,35]
var a = arry.find(function(el){
return el > 15
})
console.log(a) //25
arr.findIndex()
找出第一個(gè)符合條件的數(shù)組元素的位置,沒有找到返回-1
var arry = [1,25,84,35]
var a = arry.findIndex(function(el){
return el > 25
})
console.log(a) //2
arr.fill()
一個(gè)固定值填充一個(gè)數(shù)組中從起始索引到終止索引內(nèi)的全部元素
fill 方法接受三個(gè)參數(shù)
value
,start
以及end
.start
和end
參數(shù)是可選的, 其默認(rèn)值分別為0
和this
對(duì)象的length 屬性值
.
如果 start
是個(gè)負(fù)數(shù), 則開始索引會(huì)被自動(dòng)計(jì)算成為 length+start, 其中
length
是 this
對(duì)象的 length 屬性值
. 如果 end
是個(gè)負(fù)數(shù), 則結(jié)束索引會(huì)被自動(dòng)計(jì)算成為 length+end
.
arr.fill(value)
arr.fill(value, start)
arr.fill(value, start, end)
var arr1 = ["a","b","c","d"]
arr1.fill("x")
console.log(arr1) //["x", "x", "x", "x"] 只有value 默認(rèn)start為0 end為length
arr1.fill("x",1) //["a"衣摩,"x","x","x"];
arr1.fill("x",-1) //["a","b","c","x"];
//過界的話昂验,數(shù)組沒有變化
for of
對(duì)象沒有迭代器捂敌,需要自己去建
var arr = ["a","b","c","d"]
for(let v of arr) {
console.log(v) //a b c d
}
for下數(shù)組擴(kuò)展
//arr.keys() 用于for of對(duì)數(shù)組鍵名的遍歷
var arr = ["a","b","c","d"]
for(let v of arr.keys()) {
console.log(v) //0 1 2 3
}
//arr.values() 用于for of對(duì)數(shù)組鍵值的遍歷
//*目前瀏覽器未實(shí)現(xiàn)該方法
//arr.entries()用于for of對(duì)數(shù)組鍵值對(duì)的遍歷
var arr = ["a","b","c"]
for (var [index,ele] of arr.entries()) {
console.log(index,ele)
// 0 "a" ,1 "b" ,2 "c"
}
數(shù)據(jù)結(jié)構(gòu)Set
在es6中新增了一個(gè)新的數(shù)據(jù)結(jié)構(gòu) - 集合,
Set是一個(gè)構(gòu)造函數(shù)艾扮,可以不傳入任何參數(shù)創(chuàng)建一個(gè)空的集合,也可以傳入一個(gè)數(shù)組對(duì)其進(jìn)行初始化
let s1 = new Set(["a","b","c"])
Set和數(shù)組類似占婉,但是Set的值是唯一的不重復(fù)的
- 不能通過下標(biāo)進(jìn)行取值
- 集合是一個(gè)可迭代的對(duì)象
- 有一個(gè)屬性:size泡嘴,類似數(shù)組的length屬性
set.add(value) 為Set的實(shí)例添加值
s1.add('d');
console.log(s1); //a b c d
set.delete(value) 刪除Set實(shí)例的值
s1.delete("a");
console.log(s1); //b c d
set.has(value) 判斷傳入的參數(shù)是否為set的成員
s1.has("b") //true
s1.has("x") //false
set.clear()清除set中所有成員
s1.clear(); {}
Map()
ES6提供了map數(shù)據(jù)結(jié)構(gòu)。它類似于對(duì)象逆济,也是鍵值對(duì)的集合酌予,但是“鍵”的范圍不限于字符串,各種類型的值(包括對(duì)象)都可以當(dāng)作鍵奖慌。
Map也可以接受一個(gè)數(shù)組作為參數(shù)抛虫。該數(shù)組的成員是一個(gè)個(gè)表示鍵值對(duì)的數(shù)組。
var arr = [1,2,3]
let m1 = new Map();
m1.set('a', 1); //{"a"=> 1}
m1.set('b', 1);
m1.set(arr, 'abc'); //{"a" => 1, "b" => 1, (3) [1, 2, 3] => "abc"}
get() 通過key獲取value
m1.get("a") //1
其他的set添加新的鍵值對(duì)简僧,has()查找key是否存在建椰,delete(),clear()方法和上面set使用方法一樣的
Class
糖衣語法,和構(gòu)造函數(shù)本質(zhì)上沒有區(qū)別
//之前構(gòu)造函數(shù)
function Creat(name,age){
this.name = name;
this.age = age;
}
Creat.prototype.say = function(){
console.log(`我叫${this.name},我今年${this.age}歲`)
}
var c1 = new Creat("小明",18)
c1.say() //我叫小明,我今年18歲
Class語法
class Creat{
constructor(name,age) {
this.name = name;
this.age = age;
}
say(){
console.log(`我叫${this.name},我今年${this.age}歲`)
}
}
var c1 = new Creat("小李",24)
c1.say() //我叫小李,我今年24歲
當(dāng)我們通過class模式創(chuàng)建擁有不同類型的對(duì)象時(shí)岛马,就可以extends(繼承)
class Occ extends Creat { //Occ繼承Creat
constructor(name,age,type){
//super -> 父類構(gòu)造器
super(name,age); //相當(dāng)Creat.call(this)
this.type = type
}
}
var c2 = new Occ("小王",26,"老師")
c2.say();// 我叫小王,我今年26歲
c2.myJob()// 我的職業(yè)是司機(jī)