JavaScript 小技巧

介绍一些不常用 但是很有用的js技巧

1. 使用for创建死循环

//举例 for example
for
(;;){ }

2.使用math.max.apply 取出数组中的最大值

//举例 for example

var arr = [1, 2, 3, 4, 5, "6"];  // 可以输入字符串类型的数字

var maxNum = Math.max.apply(null, arr);

console.log(maxNum);    //返回值为 6

3.使用eval()方法灵活创建js变量,在例如正则表达式中写入变量

//举例 for example
var str = "abc";
var arr = ["a", "b", "c"];
console.log(str.match(/arr[0]/gi)); //返回结果 null
//使用eval()方法
console.log(str.match(eval("/"+arr[0]+"/gi"))); //返回结果 ["a"]

 4.使用+号获取getTime()毫秒数

var date = new Date();
console.log(date.getTime());  //返回值: 1544355249546
var dates = +new Date();
console.log(dates);      //返回值:1544355249546
console.log(Date.now()) //返回值:
1544355249546

未完,待续。。。

原文地址:https://www.cnblogs.com/CooLLYP/p/10080024.html