正文
在这篇文章中将给大家分享12个有关于JavaScript的小技巧。这些小技巧可能在你的实际工作中或许能帮助你解决一些问题。
使用!!
操作符转换布尔值
有时候我们需要对一个变量查检其是否存在或者检查值是否有一个有效值,如果存在就返回true
值。为了做这样的验证,我们可以使用!!
操作符来实现是非常的方便与简单。对于变量可以使用!!variable
做检测,只要变量的值为:0
、null
、" "
、undefined
或者NaN
都将返回的是false
,反之返回的是true
。比如下面的示例:
?
<span class = "hljs-function" ><span class = "hljs-keyword" > function <span class = "hljs-title" >Account(<span class = "hljs-params" >cash) { <span class = "hljs-keyword" > this .cash = cash; <span class = "hljs-keyword" > this .hasMoney = !!cash; } <span class = "hljs-keyword" > var account = <span class = "hljs-keyword" > new Account(<span class = "hljs-number" >100.50); <span class = "hljs-built_in" >console.log(account.cash); <span class = "hljs-comment" > // 100.50 <span class = "hljs-built_in" >console.log(account.hasMoney); <span class = "hljs-comment" > // true <span class = "hljs-keyword" > var emptyAccount = <span class = "hljs-keyword" > new Account(<span class = "hljs-number" >0); <span class = "hljs-built_in" >console.log(emptyAccount.cash); <span class = "hljs-comment" > // 0 <span class = "hljs-built_in" >console.log(emptyAccount.hasMoney); <span class = "hljs-comment" > // false</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> |
在这个示例中,只要account.cash
的值大于0
,那么account.hasMoney
返回的值就是true
。
使用+
将字符串转换成数字
这个技巧非常有用,其非常简单,可以交字符串数据转换成数字,不过其只适合用于字符串数据,否则将返回NaN
,比如下面的示例:
?
<span class = "hljs-function" ><span class = "hljs-keyword" > function <span class = "hljs-title" >toNumber(<span class = "hljs-params" >strNumber) { <span class = "hljs-keyword" > return +strNumber; } <span class = "hljs-built_in" >console.log(toNumber(<span class = "hljs-string" > "1234" )); <span class = "hljs-comment" > // 1234 <span class = "hljs-built_in" >console.log(toNumber(<span class = "hljs-string" > "ACB" )); <span class = "hljs-comment" > // NaN</span></span></span></span></span></span></span></span></span></span></span> |
这个也适用于Date
,在本例中,它将返回的是时间戳数字:
?
<span class = "hljs-built_in" >console.log(+<span class = "hljs-keyword" > new <span class = "hljs-built_in" >Date()) <span class = "hljs-comment" > // 1461288164385</span></span></span></span> |
并条件符
如果你有一段这样的代码:
?
<span class = "hljs-keyword" > if (conected) { login(); }</span> |
你也可以将变量简写,并且使用&&
和函数连接在一起,比如上面的示例,可以简写成这样:
?
<span class = "hljs-function" >conected && <span class = "hljs-title" >login<span class = "hljs-params" >();</span></span></span> |
如果一些属性或函数存在于一个对象中,你也可以这样做检测,如下面的代码所示:
?
<span class = "hljs-selector-tag" >user && <span class = "hljs-selector-tag" >user<span class = "hljs-selector-class" >.login();</span></span></span> |
使用||
运算符
在ES6中有默认参数这一特性。为了在老版本的浏览器中模拟这一特性,可以使用||
操作符,并且将将默认值当做第二个参数传入。如果第一个参数返回的值为false
,那么第二个值将会认为是一个默认值。如下面这个示例:
?
<span class = "hljs-function" ><span class = "hljs-keyword" > function <span class = "hljs-title" >User(<span class = "hljs-params" >name, age) { <span class = "hljs-keyword" > this .name = name || <span class = "hljs-string" > "Oliver Queen" ; <span class = "hljs-keyword" > this .age = age || <span class = "hljs-number" >27; } <span class = "hljs-keyword" > var user1 = <span class = "hljs-keyword" > new User(); <span class = "hljs-built_in" >console.log(user1.name); <span class = "hljs-comment" > // Oliver Queen <span class = "hljs-built_in" >console.log(user1.age); <span class = "hljs-comment" > // 27
<span class = "hljs-keyword" > var user2 = <span class = "hljs-keyword" > new User(<span class = "hljs-string" > "Barry Allen" , <span class = "hljs-number" >25); <span class = "hljs-built_in" >console.log(user2.name); <span class = "hljs-comment" > // Barry Allen <span class = "hljs-built_in" >console.log(user2.age); <span class = "hljs-comment" > // 25</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> |
在循环中缓存array.length
这个技巧很简单,这个在处理一个很大的数组循环时,对性能影响将是非常大的。基本上,大家都会写一个这样的同步迭代的数组:
?
<span class = "hljs-keyword" > for (<span class = "hljs-keyword" > var i = <span class = "hljs-number" >0; i < <span class = "hljs-keyword" >array.length; i++) { console.log(<span class = "hljs-keyword" >array[i]); }</span></span></span></span></span> |
如果是一个小型数组,这样做很好,如果你要处理的是一个大的数组,这段代码在每次迭代都将会重新计算数组的大小,这将会导致一些延误。为了避免这种现象出现,可以将array.length
做一个缓存:
?
<span class = "hljs-keyword" > var length = <span class = "hljs-keyword" >array.length; <span class = "hljs-keyword" > for (<span class = "hljs-keyword" > var i = <span class = "hljs-number" >0; i < length; i++) { console.log(<span class = "hljs-keyword" >array[i]); }</span></span></span></span></span></span> |
你也可以写在这样:
?
<span class = "hljs-keyword" > for (<span class = "hljs-keyword" > var i = <span class = "hljs-number" >0, length = <span class = "hljs-keyword" >array.length; i < length; i++) { console.log(<span class = "hljs-keyword" >array[i]); }</span></span></span></span></span> |
检测对象中属性
当你需要检测一些属性是否存在,避免运行未定义的函数或属性时,这个小技巧就显得很有用。如果你打算定些一些跨兼容的浏览器代码,你也可能会用到这个小技巧。例如,你想使用document.querySelector()
来选择一个id
,并且让它能兼容IE6浏览器,但是在IE6浏览器中这个函数是不存在的,那么使用这个操作符来检测这个函数是否存在就显得非常的有用,如下面的示例:
?
<span class = "hljs-keyword" > if (<span class = "hljs-string" > 'querySelector' <span class = "hljs-keyword" > in <span class = "hljs-built_in" >document) { <span class = "hljs-built_in" >document.querySelector(<span class = "hljs-string" > "#id" ); } <span class = "hljs-keyword" > else { <span class = "hljs-built_in" >document.getElementById(<span class = "hljs-string" > "id" ); }</span></span></span></span></span></span></span></span></span> |
在这个示例中,如果document
不存在querySelector
函数,那么就会调用docuemnt.getElementById("id")
。
获取数组中最后一个元素
Array.prototype.slice(begin,end)
用来获取begin
和end
之间的数组元素。如果你不设置end
参数,将会将数组的默认长度值当作end
值。但有些同学可能不知道这个函数还可以接受负值作为参数。如果你设置一个负值作为begin
的值,那么你可以获取数组的最后一个元素。如:
?
var <span class = "hljs-built_in" >array = [<span class = "hljs-number" >1,<span class = "hljs-number" >2,<span class = "hljs-number" >3,<span class = "hljs-number" >4,<span class = "hljs-number" >5,<span class = "hljs-number" >6]; console.<span class = "hljs-built_in" >log(<span class = "hljs-built_in" >array.slice(<span class = "hljs-number" >-1)); <span class = "hljs-comment" > // [6] console.<span class = "hljs-built_in" >log(<span class = "hljs-built_in" >array.slice(<span class = "hljs-number" >-2)); <span class = "hljs-comment" > // [5,6] console.<span class = "hljs-built_in" >log(<span class = "hljs-built_in" >array.slice(<span class = "hljs-number" >-3)); <span class = "hljs-comment" > // [4,5,6]</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> |
数组截断
这个小技巧主要用来锁定数组的大小,如果用于删除数组中的一些元素来说,是非常有用的。例如,你的数组有10
个元素,但你只想只要前五个元素,那么你可以通过array.length=5
来截断数组。如下面这个示例:
?
var <span class = "hljs-built_in" >array = [<span class = "hljs-number" >1,<span class = "hljs-number" >2,<span class = "hljs-number" >3,<span class = "hljs-number" >4,<span class = "hljs-number" >5,<span class = "hljs-number" >6]; console.<span class = "hljs-built_in" >log(<span class = "hljs-built_in" >array.length); <span class = "hljs-comment" > // 6 <span class = "hljs-built_in" >array.length = <span class = "hljs-number" >3; console.<span class = "hljs-built_in" >log(<span class = "hljs-built_in" >array.length); <span class = "hljs-comment" > // 3 console.<span class = "hljs-built_in" >log(<span class = "hljs-built_in" >array); <span class = "hljs-comment" > // [1,2,3]</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> |
替换所有
String.replace()
函数允许你使用字符串或正则表达式来替换字符串,本身这个函数只替换第一次出现的字符串,不过你可以使用正则表达多中的/g
来模拟replaceAll()
函数功能:
?
<span class = "hljs-keyword" > var string = <span class = "hljs-string" > "john john" ; <span class = "hljs-built_in" >console.log(string.replace(<span class = "hljs-regexp" >/hn/, <span class = "hljs-string" > "ana" )); <span class = "hljs-comment" > // "joana john" <span class = "hljs-built_in" >console.log(string.replace(<span class = "hljs-regexp" >/hn/g, <span class = "hljs-string" > "ana" )); <span class = "hljs-comment" > // "joana joana"</span></span></span></span></span></span></span></span></span></span> |
合并数组
如果你要合并两个数组,一般情况之下你都会使用Array.concat()
函数:
?
<span class = "hljs-keyword" > var array1 = [<span class = "hljs-number" >1,<span class = "hljs-number" >2,<span class = "hljs-number" >3]; <span class = "hljs-keyword" > var array2 = [<span class = "hljs-number" >4,<span class = "hljs-number" >5,<span class = "hljs-number" >6]; <span class = "hljs-built_in" >console.log(array1.concat(array2)); <span class = "hljs-comment" > // [1,2,3,4,5,6];</span></span></span></span></span></span></span></span></span></span> |
然后这个函数并不适合用来合并两个大型的数组,因为其将消耗大量的内存来存储新创建的数组。在这种情况之个,可以使用Array.pus().apply(arr1,arr2)
来替代创建一个新数组。这种方法不是用来创建一个新的数组,其只是将第一个第二个数组合并在一起,同时减少内存的使用:
?
<span class = "hljs-keyword" > var array1 = [<span class = "hljs-number" >1,<span class = "hljs-number" >2,<span class = "hljs-number" >3]; <span class = "hljs-keyword" > var array2 = [<span class = "hljs-number" >4,<span class = "hljs-number" >5,<span class = "hljs-number" >6]; <span class = "hljs-built_in" >console.log(array1.push.apply(array1, array2)); <span class = "hljs-comment" > // [1,2,3,4,5,6];</span></span></span></span></span></span></span></span></span></span> |
将NodeList
转换成数组
如果你运行document.querySelectorAll(“p”)
函数时,它可能返回DOM元素的数组,也就是NodeList
对象。但这个对象不具有数组的函数功能,比如sort()
、reduce()
、map()
、filter()
等。为了让这些原生的数组函数功能也能用于其上面,需要将节点列表转换成数组。可以使用[].slice.call(elements)
来实现:
?
<span class = "hljs-keyword" > var elements = <span class = "hljs-built_in" >document.querySelectorAll(<span class = "hljs-string" > "p" ); <span class = "hljs-comment" > // NodeList <span class = "hljs-keyword" > var arrayElements = [].slice.call(elements); <span class = "hljs-comment" > // Now the NodeList is an array <span class = "hljs-keyword" > var arrayElements = <span class = "hljs-built_in" >Array.from(elements); <span class = "hljs-comment" > // This is another way of converting NodeList to Array</span></span></span></span></span></span></span></span></span> |
数组元素的洗牌
对于数组元素的洗牌,不需要使用任何外部的库,比如Lodash,只要这样做:
?
<span class = "hljs-keyword" > var <span class = "hljs-keyword" >list = [<span class = "hljs-number" >1,<span class = "hljs-number" >2,<span class = "hljs-number" >3]; console.log(<span class = "hljs-keyword" >list.sort(<span class = "hljs-function" ><span class = "hljs-keyword" > function <span class = "hljs-params" >() { Math.random() - <span class = "hljs-number" >0.5 })); <span class = "hljs-comment" > // [2,1,3]</span></span></span></span></span></span></span></span></span></span></span> |
总结
现在你学会了些有用的JavaScript小技巧。希望这些小技巧能在工作中帮助你解决一些麻烦,或者说这篇文章对你有所帮助。如果你有一些优秀的JavaScript小技巧,欢迎在评论中与我们一起分享。
【我有一个前端学习交流QQ群:328058344 如果你在学习前端的过程中遇到什么问题,欢迎来我的QQ群提问,群里每天还会更新一些学习资源。禁止闲聊,非喜勿进。】