脚本宝典收集整理的这篇文章主要介绍了在不到5分钟的时间内学习这些精巧的JavaScript技巧,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
const arr = [11, 22, 33, 44, 55, 66];
// truncanting
arr.length = 3;
console.LOG(arr); //=> [11, 22, 33]
// clearing
arr.length = 0;
console.log(arr); //=> []
console.log(arr[2]); //=> undefined
DOSomething({ foo: 'Hello', bar: 'Hey!', baz: 42 });
function doSomething(config) {
const foo = config.foo !== undefined ? config.foo : 'Hi';
const bar = config.bar !== undefined ? config.bar : 'Yo!';
const baz = config.baz !== undefined ? config.baz : 13;
// ...
}
这是一个古老但有效的模式,它试图模拟JavaScript中的命名参数。函数调用看起来很好。另一方面,配置对象处理逻辑是不必要冗长的。用ES2015对象破坏,你可以规避这个缺点:
This is an old but effective pattern, which tries to simulate named parameters in JavaScript. The function calling looks fine. On the other hand, the config object handling logic is unnecessarily verbose. WITh ES2015 object destructuring, you can circumvent this downside:
function doSomething({ foo = 'Hi', bar = 'Yo!', baz = 13 }) {
// ...
}
如果你需要使配置对象是可选的,那么它也是非常简单的:
And if you need to make the config object optional, it’s very simple, too:
function doSomething({ foo = 'Hi', bar = 'Yo!', baz = 13 } = {}) {
// ...
}
const csvFileLine = '1997,John Doe,US,john@doe.COM,New York';
const { 2: country, 4: state } = csvFileLine.split(',');
function getWaterState(tempInCelsius) {
let state;
switch (true) {
case (tempInCelsius <= 0):
state = 'Solid';
break;
case (tempInCelsius > 0 && tempInCelsius < 100):
state = 'Liquid';
break;
default:
state = 'Gas';
}
return state;
}
await PRomise.all([anAsyncCall(), thisIsAlSOAsync(), oneMore()])
const pureObject = Object.create(null);
console.log(pureObject); //=> {}
console.log(pureObject.constructor); //=> undefined
console.log(pureObject.toString); //=> undefined
console.log(pureObject.hasOwnProperty); //=> undefined
const obj = {
foo: { bar: [11, 22, 33, 44], baz: { bing: true, boom: 'Hello' } }
};
// The third parameter is the number of spaces used to
// beautify the JSON output.
JSON.stringify(obj, null, 4);
// =>"{
// => "foo": {
// => "bar": [
// => 11,
// => 22,
// => 33,
// => 44
// => ],
// => "baz": {
// => "bing": true,
// => "boom": "Hello"
// => }
// => }
// =>}"
const removeDuplicateitems = arr => [...new Set(arr)];
removeDuplicateItems([42, 'foo', 42, 'foo', true, true]);
//=> [42, "foo", true]
const arr = [11, [22, 33], [44, 55], 66];
const flatArr = [].concat(...arr); //=> [11, 22, 33, 44, 55, 66]
Unfortunately, the above trick will only work with bidimensional arrays. But with recursive calls, we can make it suitable for arrays with more than 2 dimensions:
function flattenArray(arr) {
const flattened = [].concat(...arr);
return flattened.some(item => Array.isArray(item)) ?
flattenArray(flattened) : flattened;
}
const arr = [11, [22, 33], [44, [55, 66, [77, [88]], 99]]];
const flatArr = flattenArray(arr);
//=> [11, 22, 33, 44, 55, 66, 77, 88, 99]
And there you have it! I hope these neat liTTLe tricks help you write better and more beautiful JavaScript.
觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!以上是脚本宝典为你收集整理的在不到5分钟的时间内学习这些精巧的JavaScript技巧全部内容,希望文章能够帮你解决在不到5分钟的时间内学习这些精巧的JavaScript技巧所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。