新手做網(wǎng)站教程seo關(guān)鍵詞布局案例
文章目錄
- 1 類新增特性
- 1.1 私有屬性和方法
- 1.2 靜態(tài)成員的私有屬性和方法
- 1.3 靜態(tài)代碼塊
- 1.4 使用in來判斷某個(gè)對(duì)象是否擁有某個(gè)私有屬性
- 2 支持在最外層寫await
- 3 at函數(shù)來索引元素
- 4 正則匹配的開始和結(jié)束索引
- 5 findLast() 和 findLastIndex() 函數(shù)
- 6 Error對(duì)象的Cause屬性
1 類新增特性
1.1 私有屬性和方法
class Cache{#obj = {}get(key){return this.#obj[key]}set(key, value){this.#obj[key] = value}
}let cache = new Cache()
cache.set("name", "kerwin")
class Person{//不需要外部傳參進(jìn)來的,一開始就初始化的,可以在類的最外層作用域聲明這個(gè)成員state = { // es13可以直接提出來寫a: 1,b: 2}// a = 1;// b = 2;constructor(name, age){this.name = namethis.age = age// this.state = { // 不需要傳參// a: 1,// b: 2// }}
}
1.2 靜態(tài)成員的私有屬性和方法
我們還可以給類定義靜態(tài)成員和靜態(tài)私有函數(shù)。類的靜態(tài)方法可以使用this
關(guān)鍵字訪問其他的私有或者公有靜態(tài)成員。
class Cache{static #count = 0; // Cache.#count訪問不到,報(bào)錯(cuò)static getCount(){return this.#count // Cache.#getCount訪問不到,報(bào)錯(cuò)}#obj = {}get(key){return this.#obj[key]}set(key, value){this.#obj[key] = value}
}let cache = new Cache()
cache.set("name", "kerwin")console.log(Cache.getCount())
1.3 靜態(tài)代碼塊
ES13允許在類中通過static
關(guān)鍵字定義一系列靜態(tài)代碼塊,這些代碼塊只會(huì)在類被創(chuàng)造的時(shí)候執(zhí)行一次。這其實(shí)有點(diǎn)像一些其他的如C#和Java等面向?qū)ο蟮木幊陶Z(yǔ)言的靜態(tài)構(gòu)造函數(shù)的用法。
一個(gè)類可以定義任意多的靜態(tài)代碼塊,這些代碼塊會(huì)和穿插在它們之間的靜態(tài)成員變量一起按照定義的順序在類初始化的時(shí)候執(zhí)行一次。我們還可以使用super
關(guān)鍵字來訪問父類的屬性。
class Cache{static obj = new Map()static {this.obj.set("name","kerwin")this.obj.set("age",100)}static{console.log(this.obj)}
}console.log(Cache.obj)
1.4 使用in來判斷某個(gè)對(duì)象是否擁有某個(gè)私有屬性
class Cache {#obj = {}get(key) {return this.#obj[key]}set(key, value) {this.#obj[key] = value}hasObj(){return #obj in this // in關(guān)鍵字:判斷某個(gè)屬性是不是私有屬性}
}let cache = new Cache()
console.log(cache.hasObj()) // true
2 支持在最外層寫await
頂層await
只能用在ES6模塊,不能用在CommonJS模塊。這是因?yàn)镃ommonJS模塊的require()
是同步加載,如果有頂層await
,就沒法處理加載了。
<script type="module">function ajax() {return new Promise((resolve) => {setTimeout(() => {resolve("data-1111");}, 1000);})
}let res = await ajax();
console.log(res)
</script>
//1.js
function ajax(){return new Promise((resolve)=>{setTimeout(()=>{resolve("data-11111")},2000)})
}let data = await ajax() // 2秒之后模塊才會(huì)導(dǎo)出export default {name:"moduleA",data
}<script type="module">console.log("開始")// await寫起來是同步,執(zhí)行是異步的感覺let moduleA = await import("./1.js") // 動(dòng)態(tài)導(dǎo)入,導(dǎo)入promise對(duì)象console.log(moduleA) // 2秒之后拿到結(jié)果,不耽誤上面代碼執(zhí)行
</script>
3 at函數(shù)來索引元素
let arr = ["kerwin","tiechui","gangdan","xiaoming"]console.log(arr[1])
console.log(arr[arr.length - 1])
console.log(arr[arr.length - 2]) console.log(arr.at(1))
console.log(arr.at(-1))
console.log(arr.at(-2))let str = "kerwin"console.log(str.at(-1))
console.log(str.at(-2))
4 正則匹配的開始和結(jié)束索引
let str = "今天是2022-11-10"
// 多了一個(gè)屬性:indices:0: [3, 13], 1: [3, 7], 2: [8, 10], 3: [11, 13]
let reg = /(?<year>[0 - 9]{4}) - (?<month>[0 - 9]{2}) - (?<day>[0 - 9]{2})/d//exec
let res = reg.exec(str)
// console.log(res)
let {year, month, day} = res.groups // group -> index = 3
console.log(res) // day: [11, 13], month: [8, 10], year: [3, 7]
5 findLast() 和 findLastIndex() 函數(shù)
let arr = [11,12,13,14,15]// let res = arr.find(function(value){
// return value % 2 === 0
// })
// let res = arr.findIndex(function(value){
// return value % 2 === 0
// })
// let res = arr.findLast(function(value){
// return value % 2 === 0
// })
let res = arr.findLastIndex(function(value){return value % 2 === 0
})let res1 = arr.find(value => value > 13) // 14
let res2 = arr.findIndex(value => value > 13) // 3
let res3 = arr.findLast(value => value > 13) // 15
let res4 = arr.findLastIndex(value => value > 13) // 4let res5= arr.find(value => value % 2 === 0) // 12
let res= arr.findLast(value => value % 2 === 0) // 14
console.log(res)
6 Error對(duì)象的Cause屬性
Error對(duì)象多了一個(gè)cause
屬性來指明錯(cuò)誤出現(xiàn)的原因。這個(gè)屬性可以幫助我們?yōu)殄e(cuò)誤添加更多的上下文信息,從而幫助使用者們更好地定位錯(cuò)誤。
function getData(){try{console.log(kerwin)}catch(e){throw new Error('New error 1111111',{cause:"這是因?yàn)?,,,,,,,,"});}
}try{getData()
}catch(e){console.log(e.cause)
}