亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

一文詳解如何在項(xiàng)目中和平時(shí)練習(xí)中應(yīng)用es6語法

 更新時(shí)間:2022年11月10日 12:08:38   作者:Cirrod  
ES6是JavaScript的一個(gè)版本,因?yàn)槲覀兦懊嬗玫降膙ue默認(rèn)使用ES6語法開發(fā),所以我們在這一節(jié)補(bǔ)充ES6的知識(shí)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于如何在項(xiàng)目中和平時(shí)練習(xí)中應(yīng)用es6語法的相關(guān)資料,需要的朋友可以參考下

一、關(guān)于取值

const obj = {
    a:1,
    b:2,
    c:3,
    d:4,
    e:5,
}
 
const a = obj.a;
const b = obj.b;
const c = obj.c;
const d = obj.d;
const e = obj.e;

es6解構(gòu)賦值:

const {a,b,c,d,e} = obj;

也可以使用:進(jìn)行重命名

const {a:a1} = obj;
console.log(a1)

二、數(shù)組和對象的合并

const a = [1,2,3];
const b = [1,5,6];
const c = a.concat(b);//[1,2,3,1,5,6]
 
const obj1 = {
  a:1,
}
const obj2 = {
  b:1,
}
const obj = Object.assign({}, obj1, obj2);//{a:1,b:1}

es6的擴(kuò)展運(yùn)算符,數(shù)組的合去重

const a = [1,2,3];
const b = [1,5,6];
const c = [...new Set([...a,...b])];//[1,2,3,5,6]
 
const obj1 = {
  a:1,
}
const obj2 = {
  b:1,
}
const obj = {...obj1,...obj2};//{a:1,b:1}

三、字符串拼接

const name = '小明';
const score = 59;
let result = '';
if(score > 60){
  result = `${name}的考試成績及格`; 
}else{
  result = `${name}的考試成績不及格`; 
}

es6模板字符串

${}中可以放入任意的JavaScript表達(dá)式,可以進(jìn)行運(yùn)算,以及引用對象屬性。

const name = '小明';
const score = 59;
const result = `${name}${score > 60?'的考試成績及格':'的考試成績不及格'}`;

四、if條件多

if(
    type == 1 ||
    type == 2 ||
    type == 3 ||
    type == 4 ||
){
   //...
}

es6數(shù)組方法includes

const condition = [1,2,3,4];
 
if( condition.includes(type) ){
   //...
}

五、精確搜索

const a = [1,2,3,4,5];
const result = a.filter( 
  item =>{
    return item === 3
  }
)

es6數(shù)組find方法,性能優(yōu)化,find方法中找到符合條件的項(xiàng),就不會(huì)繼續(xù)遍歷數(shù)組。

const a = [1,2,3,4,5];
const result = a.find( 
  item =>{
    return item === 3
  }
)

六、獲取對象屬性值

const name = obj && obj.name;

es6可選鏈?

const name = obj?.name;

可選鏈?補(bǔ)充:

會(huì)在嘗試訪問obj.name之前確定obj既不是null也不是undefined,如果obj是null或者undefined表達(dá)式會(huì)進(jìn)行短路計(jì)算直接返回undefined

七、輸入框非空判斷

if(value !== null && value !== undefined && value !== ''){
    //...
}

es6空位合并運(yùn)算符??

if((value??'') !== ''){
  //...
}

es6空位合并運(yùn)算符??補(bǔ)充:

前值是null或者undefined時(shí)表達(dá)式返回后值

const a = 0 ?? '默認(rèn)值a';
cosnt b = null ?? '默認(rèn)值b';
 
console.log(a); // "0"  0是假值,但不是 null 或者 undefined
console.log(b); // "默認(rèn)值b"

拓展:

變量賦默認(rèn)值一般使用邏輯或操作符 || 。但是由于 || 是一個(gè)布爾邏輯運(yùn)算符,左側(cè)的操作數(shù)會(huì)被強(qiáng)制轉(zhuǎn)換成布爾值用于求值。任何假值(0, '', NaN, null, undefined)都不會(huì)被返回。這導(dǎo)致如果你使用0''NaN作為有效值,就會(huì)出現(xiàn)不可預(yù)料的后果。這也是 ?? 操作符可以解決這個(gè)問題

const a = 0;
const b = a || 5
 
 
//當(dāng)0作為有效值,與我們期望的 b 的值不一致
console.log(b); // 5

八、異步函數(shù)回調(diào)

const fn1 = () =>{
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(1);
    }, 300);
  });
}
const fn2 = () =>{
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(2);
    }, 600);
  });
}
const fn = () =>{
   fn1().then(res1 =>{
      console.log(res1);// 1
      fn2().then(res2 =>{
        console.log(res2)
      })
   })
}

es6的async和await

const fn = async () =>{
  const res1 = await fn1();
  const res2 = await fn2();
  console.log(res1);// 1
  console.log(res2);// 2
}

九、函數(shù)默認(rèn)參數(shù)

function fn (name, age) {
  let name = name || 'hsq'
  let age = age || 22
  console.log(name, age)
}
fn() // hsq 22

es6函數(shù)默認(rèn)參數(shù)

function fn (name = 'hsq', age = 22) {
  console.log(name, age)
}
fn() // hsq 22
fn('test', 23) // test 23

十、剩余參數(shù)

一個(gè)函數(shù),傳入?yún)?shù)的個(gè)數(shù)是不確定的,這就可以用ES6的剩余參數(shù)

function fn (name, ...params) {
  console.log(name)
  console.log(params)
}
fn ('hsq', 1, 2) // hsq [ 1, 2 ]
fn ('hsq', 1, 2, 3, 4, 5) // hsq [ 1, 2, 3, 4, 5 ]

十 一、箭頭函數(shù)

普通函數(shù)

function fn () {}
 
const fn = function () {}

es6箭頭函數(shù)

const fn = () => {}
 
// 如果只有一個(gè)參數(shù),可以省略括號(hào)
const fn = name => {}
 
// 如果函數(shù)體里只有一句return
const fn = name => {
    return 2 * name
}
// 可簡寫為
const fn = name => 2 * name
 
// 如果返回的是對象
const fn = name => ({ name: name })
 

普通函數(shù)和箭頭函數(shù)的區(qū)別: 

  • 1、箭頭函數(shù)不可作為構(gòu)造函數(shù),不能使用new
  • 2、箭頭函數(shù)沒有原型對象
  • 3、箭頭函數(shù)沒有自己的this
  • 4、箭頭函數(shù)沒有arguments對象

十二、獲取對象的鍵值

Object.keys

可以用來獲取對象的key的集合

const obj = {
  name: 'hsq',
  age: 22,
  gender: '男'
}
 
const values = Object.keys(obj)
console.log(values) // [ 'hsq', 22, '男' ]

Object.values

可以用來獲取對象的value的集合

const obj = {
  name: 'hsq',
  age: 22,
  gender: '男'
}
 
const values = Object.values(obj)
console.log(values) // [ 'hsq', 22, '男' ]

Object.entries

可以用來獲取對象的鍵值對集合

const obj = {
  name: 'hsq',
  age: 22,
  gender: '男'
}
 
const entries = Object.entries(obj)
console.log(entries) 
// [ [ 'name', 'hsq' ], [ 'age', 22 ], [ 'gender', '男' ] ]

十三、數(shù)組扁平化

Array.flat

有一個(gè)二維數(shù)組,我想讓他變成一維數(shù)組:

const arr = [1, 2, 3, [4, 5, 6]]
 
console.log(arr.flat()) // [ 1, 2, 3, 4, 5, 6 ]

還可以傳參數(shù),參數(shù)為降維的次數(shù)

const arr = [1, 2, 3, [4, 5, 6, [7, 8, 9]]]
 
console.log(arr.flat(2))
[
  1, 2, 3, 4, 5,
  6, 7, 8, 9
]

如果傳的是一個(gè)無限大的數(shù)字,那么就實(shí)現(xiàn)了多維數(shù)組(無論幾維)降為一維數(shù)組

const arr = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12]]]]
 
console.log(arr.flat(Infinity))
[
   1,  2, 3, 4,  5,
   6,  7, 8, 9, 10,
   11, 12
]
 

總結(jié)

到此這篇關(guān)于如何在項(xiàng)目中和平時(shí)練習(xí)中應(yīng)用es6語法的文章就介紹到這了,更多相關(guān)項(xiàng)目應(yīng)用es6語法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論