三個有用的技巧
由後往前取得陣列數值:
如果你想要由後往前取得陣列的數值,可以這麼做:
var newArray = [1, 2, 3, 4];
console.log(newArray.slice(-1)); // [4]
console.log(newArray.slice(-2)); // [3, 4]
console.log(newArray.slice(-3)); // [2, 3, 4]
console.log(newArray.slice(-4)); // [1, 2, 3, 4]
Short-circuits 狀態
如果你有一個 function,它的狀態為 true
,像是這樣:
if (condition){
dosomething();
}
你可以像這樣使用 short-circuit:
condition && dosomething();
使用「||」設定變數的預設值
如果你要在變數設定一個預設值,你可以這麼做:
var a;
console.log(a); //undefined
a = a || 'default value';
console.log(a); //default value
a = a || 'new value';
console.log(a); //default value

MEET THE NEW JSTIPS BOOK
The book to ace the JavaScript Interview.
A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.
GET THE BOOK NOW