Here is a collection of ES6 one liners to show off!
NodeList to Array:
var headings = [ ... document.querySelectorAll('h1') ];
Unique Arrays:
[ ...new Set(array) ]
Destructuring:
var {foo, bar} = {foo: "lorem", bar: "ipsum"};
// foo => lorem and bar => ipsum
Swap like a snake:
[a,b] = [b,a]
Max in array?:
Math.max(...array);
List comprehension:
[ for (value of ["Harriet", "178"]) value ].join(" was ");
// Would give us "Harriet was 178"
// BTW it was Charles Darwin's tortoise.
_map?
[{id: 1}, {id: 2}].map(x => x.id)
// [1,2]
Object.isz!
let Object.isz = (x, y) => x === y || Object.is(x, y);
//^ Credits to Mark S. Miller.
Is it a hidden file?:
isHidden = (file) => /^\.|~$/.test(file);
isHidden(".DS_STORE") // true
Repeat with me:
(0/0+"").repeat("7")+ " BatMan!"
// "NaNNaNNaNNaNNaNNaNNaN BatMan!"
If you are using some, please do let me know!Hope it did blew your mind! ;)
原文:http://h3manth.com/new/blog/2014/es6-one-liners-to-show-off/