亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

如果您想提高 JAVAScript 技能并成為更好的開發(fā)人員,那么本文適合您。本文將教您 11 個(gè)專業(yè)技巧,幫助您編寫更好的 JavaScript 代碼,你還在等什么?一起來學(xué)習(xí)吧。

1. 使用 XOR 運(yùn)算符比較數(shù)字

按位異或運(yùn)算符 (^) 對(duì)兩個(gè)操作數(shù)執(zhí)行按位異或運(yùn)算。這意味著如果位不同則返回 1,如果相同則返回 0。

const a = 1337;
const b = 69;

// nooby
a !== 69 ? console.log('Unequal') : console.log("Equal");  // Unequal
b !== 69 ? console.log('Unequal') : console.log("Equal");  // Equal


// pro
a ^ 69 ? console.log('Unequal') : console.log("Equal");    // Unequal
b ^ 69 ? console.log('Unequal') : console.log("Equal");    // Equal

2. 用數(shù)據(jù)即時(shí)創(chuàng)建和填充數(shù)組

// nooby
const array = new Array(3);
for(let i=0; i < array.length; i++){
    array[i] = i;
}


console.log(array)  // [ 0, 1, 2 ]

// pro
const filledArray = new Array(3).fill(null).map((_, i)=> (i));
console.log(filledArray)  // [ 0, 1, 2 ]

3. 使用對(duì)象中的動(dòng)態(tài)屬性

// nooby
let propertyName = "body";
let paragraph = {
    id: 1,
};
paragraph[propertyName] = "other stringy";
// { id: 1, body: 'other stringy' }
console.log(paragraph)


// pro
let propertyName = "body";
let paragraph = {
    id: 1,
    [propertyName] : "other stringy"
};
// { id: 1, body: 'other stringy' }
console.log(paragraph)

4. 輕松消除數(shù)組中的重復(fù)值

您可以使用集合消除數(shù)組中的重復(fù)值。

// nooby
let answers = [7, 13, 31, 13, 31, 7, 42];
let leftAnswers = [];
let flag = false;
for (i = 0; i< answers.length; i++) {
    for (j = 0; j < leftAnswers.length; j++) {
        if (answers[i] === leftAnswers[j]) {
            flag = true;
        }
    }
    if (flag === false) {
        leftAnswers.push(answers[i]);
    }
    flag = false;
}
//[ 7, 13, 31, 42 ]
console.log(leftAnswers)

// pro
let answers = [7, 13, 31, 13, 31, 7, 42];
let leftAnswers = Array.from(new Set(answers));
// [ 7, 13, 31, 42 ]
console.log(leftAnswers)

5. 輕松地將對(duì)象轉(zhuǎn)換為數(shù)組

您可以使用展開運(yùn)算符將數(shù)組轉(zhuǎn)換為對(duì)象。

// nooby
let arr = ["v1", "v2", "v3"];
let objFromArray = {};


for (let i = 0; i < arr.length; ++i) {
    if (arr[i] !== undefined) {
        objFromArray[i] = arr[i];
    }
}


// { '0': 'v1', '1': 'v2', '2': 'v3' }
console.log(objFromArray)


// pro
let objFromArrayPro = {...arr};


// { '0': 'v1', '1': 'v2', '2': 'v3' }
console.log(objFromArrayPro)

6. 使用邏輯運(yùn)算符進(jìn)行短路評(píng)估

您可以使用邏輯運(yùn)算符進(jìn)行短路評(píng)估,方法是使用 && 運(yùn)算符返回表達(dá)式鏈中的第一個(gè)假值或最后一個(gè)真值,或者使用 || 運(yùn)算符返回表達(dá)式鏈中的第一個(gè)真值或最后一個(gè)假值。

const dogs = true;


// nooby
if (dogs) {
    runAway();
}


// pro
dogs && runAway()


function runAway(){
    console.log('You run!');
}

7. 對(duì)象鍵維護(hù)它們的插入順序

對(duì)象鍵通過遵循一個(gè)簡(jiǎn)單的規(guī)則來維護(hù)它們的插入順序:類整數(shù)鍵按數(shù)字升序排序,而非類整數(shù)鍵根據(jù)它們的創(chuàng)建時(shí)間排序。

const character = {
    name: "Arthas",
    age: 27,
    class: "Paladin",
    profession: "Lichking",
};


// name age class profession
console.log(Object.keys(character));

8. 創(chuàng)建并填充指定大小的數(shù)組

您可以使用帶有兩個(gè)參數(shù)的 Array() 構(gòu)造函數(shù)來創(chuàng)建和填充指定大小和值的數(shù)組:大小和值,或者對(duì)空數(shù)組使用 Array.fill() 方法。

// nooby
const size = 5;
const defaultValue = 0;
const arr = []
for(let i = 0; i < size; i++){
    arr.push(defaultValue)
}
console.log(arr);


// pro
const size = 5;
const defaultValue = 0;
const arr = Array(size).fill(defaultValue);
console.log(arr); // [0, 0, 0, 0, 0]

9. 理解 JavaScript 中的 Truthy 和 Falsy 值

在布爾上下文中使用時(shí),Truthy 和 Falsy 值會(huì)隱式轉(zhuǎn)換為 true 或 false。

虛假值 => false, 0, ""(空字符串), null, undefined, &NaN

真值 => "Values", "0", {}(空對(duì)象),&[](空數(shù)組)

// pro
if(![].length){
    console.log("There is no Array...");
} else {
    console.log("There is an Array, Hooray!");
}


if(!""){
    console.log("There is no content in this string...");
} else {
    console.log("There is content in this string, Hooray!");
}

10. 用更好的參數(shù)改進(jìn)函數(shù)

不要使用單個(gè)多個(gè)參數(shù),而是使用參數(shù)對(duì)象。在函數(shù)定義中解構(gòu)它以獲得所需的屬性。

// nooby
function upload(user, resourceId, auth, files) {}


upload(...); // need to remember the order


// pro
function upload(
    { user, resourceId, auth, files } = {}
) {}


const uploadObj = {
    user: 'me',
    resourceId: uuid(),
    auth: 'token',
    files: []
}

upload(uploadObj);

11. Null 和 Undefined 在 JavaScript 中是不同的

Null 和 undefined 是兩個(gè)不同的值,表示沒有值。

  • null => 是的,這是一個(gè)值。Undefined 不是
  • 將 null 想象成在一個(gè)空盒子前面
  • 把 undefined 想象成在沒有盒子的前面
const fnExpression = (s = 'default stringy') => console.log(s);


fnExpression(undefined); // default stringy
fnExpression(); // default stringy


fnExpression(null); // null

總結(jié)

以上就是我今天想與您分享的11個(gè)關(guān)于JavaScript的專業(yè)技巧,希望您能從中學(xué)到新東西。

分享到:
標(biāo)簽:JavaScript
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定