日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
十個JavaScript開發(fā)者需要學(xué)習(xí)掌握的技巧

 

1、三元表達(dá)式

x = 10
x % 2 == 0? console.log("even") : console.log("odd");
// even

2、快速實(shí)現(xiàn)檢查數(shù)據(jù)類型

通常我們會想要檢查變量的數(shù)據(jù)類型,這里有一個函數(shù)可以讓您輕松完成!

function checkDat(tgt, type_input) {
const type = Object.prototype.toString.call(tgt).replace(/\[object (\w+)\]/, "$1").toLowerCase();
return type_input ? type === type_input : type;
}
console.log(checkDat("test")); // "string"
console.log(checkDat(1)); // "number"
console.log(checkDat(true)); // "boolean"
console.log(checkDat([], "array")); // true
console.log(checkDat({}, "array")); // false

3、檢查空數(shù)組

有一種快速的方法可以檢查數(shù)組是否為空或空!

const arr = [];
const flag = Array.isArray(arr) && !arr.length;
const arr_1 = [1];
const flag1 = Array.isArray(arr_1) && !arr_1.length;
console.log(flag);
// true
console.log(flag1);
// false

4、Short Circuit

當(dāng)變量評估為真時,使用它執(zhí)行某些操作:

function something() {
console.log("Hello");
}
const flag = false;
!flag && something();
// Hello
const flag1 = true;
!flag1 && something();
// Nothing

5、 合并數(shù)組

輕松合并兩個數(shù)組!

var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
var arr = [...arr1, ...arr2];
console.log(arr);
//[0, 1, 2, 3, 4, 5];

6、刪除數(shù)組中的重復(fù)項(xiàng)

使用 Set 在一行中刪除數(shù)組中的重復(fù)項(xiàng):

const arr = [...new Set([0, 1, 1, 2, 2])];
console.log(arr);
// arr => [0, 1, 2]

7、 沒有臨時變量的交換變量

如標(biāo)題:在一行中交換變量:

let a = 0;
let b = 1;
[a, b] = [b, a];
console.log(a);
// a = 1
console.log(b);
// b = 0

8、過濾掉空值

在一行中過濾掉所有與 null 相關(guān)的值(Nah、undefined、null、")!:

const arr = [undefined, null, "", 0, false, NaN, 1, 2, "String"].filter(Boolean);
console.log(arr);
// [ 1, 2, 'String' ]

9、合并對象

我們不僅可以在一行中合并兩個數(shù)組,還可以對對象進(jìn)行合并!

const obj1 = { a: 0, b: 1, c: 2 };
const obj2 = { c: 3, d: 4, e: 5 };
const obj = { ...obj1, ...obj2 };
console.log(obj)
// { a: 0, b: 1, c: 3, d: 4, e: 5 }

10、一行生成隨機(jī)數(shù)

讓我們在一行中生成隨機(jī)數(shù):

const RandomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const num = RandomNum(1, 10);
console.log(num);
// 6 for example

寫在最后

以上就是我今天跟你分享的10關(guān)于JavaScript的技巧,希望你能學(xué)到一些新知識,并且將它應(yīng)用到你的實(shí)際開發(fā)中!

如果你覺得今天內(nèi)容對你有幫助,請記得點(diǎn)贊我,關(guān)注我,并將它分享給你身邊的朋友,也許能夠幫助到他。

最后,感謝你的閱讀。


網(wǎng)站名稱:十個JavaScript開發(fā)者需要學(xué)習(xí)掌握的技巧
網(wǎng)頁路徑:http://m.5511xx.com/article/cccpiip.html