brickspert / blog

个人技术博客,博文写在 Issues 里。
4.07k stars 548 forks source link

最优美的JS代码 #4

Closed brickspert closed 4 years ago

brickspert commented 6 years ago

记录看到的最优美的JS代码

brickspert commented 6 years ago

求数组中最大项

var arr = [1, 20, 22, 50, 11, 22];
var max = Math.max.apply(Math, arr);
yueshuiniao commented 6 years ago

求数组中最大项

var arr = [1, 20, 22, 50, 11, 22];
var max = Math.max(...arr);
brickspert commented 6 years ago

前端错误记录到后端

function logError(sev, msg){
        var img = new Image();
        img.src = "log.php?sev=" + encodeURIComponent(sev) + "&msg=" +
                  encodeURIComponent(msg);
}
hazeFlame commented 6 years ago

去重 Array.from(new Set([1,2,3,1,2,1,1,1,1,1]))

yueshuiniao commented 6 years ago

去重

[...new Set([1,2,3,1,2,1,1,1,1,1])]
blockmood commented 6 years ago

准确检测数据类型 Object.prototype.toString.call(val) == '[object ...]'

Miller547719886 commented 6 years ago

最大值这样也行 Math.max.call(...arr)

Misaka-0x447f commented 6 years ago

@Miller547719886 .call/.apply没啥区别的