三个实用的javascript小技巧
从后向前获取数组元素
如果你想从后向前获取一个数组的元素,可以这样写:
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]
短路条件句
如果你想在某个条件逻辑值为true
时,执行某个函数,就像这样:
if (condition) {
dosomething()
}
这时,你可以这样子运用短路:
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
You no longer need 10+ years of experience to get your dream job.
Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.
GET THE BOOK NOW
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