JavaScript可以实现很多令人惊奇的事! 从复杂的框架到处理API,有太多的东西可以学习。 甚至,仅用一行代码,它也能完成一些很棒的工作。 不信?那么请看这13条JavaScript单行代码,用上它们,会让你看起来特别专业! 1. 获取随机布尔值(true/false)
使用
以下JS代码块显示了如何使用 const randomBoolean = () => Math.random() >= 0.5; console.log(randomBoolean()); // Result: a 50/50 change on returning true of false 2. 检查指定日期是否是工作日我们可以通过此方法来检查函数中提供的日期是工作日还是周末。 以下JS代码块显示了如何编写一个返回指定日期是工作日还是周末的函数。 const isWeekday = (date) => date.getDay() % 6 !== 0; console.log(isWeekday(new Date(2021, 0, 11))); // Result: true (Monday) console.log(isWeekday(new Date(2021, 0, 10))); // Result: false (Sunday) 3. 反转字符串
有两种不同的方式可以反转字符串。使用 以下JS代码块显示了如何反转字符串。 const reverse = str => str.split('').reverse().join(''); reverse('hello world'); // Result: 'dlrow olleh' 4. 检查当前tab是否在视图/焦点中
我们可以使用
以下JS代码块显示了如何使用文档的 const isBrowserTabInView = () => document.hidden; isBrowserTabInView(); // Result: returns true or false depending on if tab is in view / focus 5. 检查数字是偶数还是奇数
可以通过使用取模运算符( 以下JS代码块显示了如何使用模运算符来检查数字是偶数还是奇数。 const isEven = num => num % 2 === 0; console.log(isEven(2)); // Result: true console.log(isEven(3)); // Result: false 6. 从日期中获取时间
通过使用
以下JS代码块显示了如何通过 const timeFromDate = date => date.toTimeString().slice(0, 8); console.log(timeFromDate(new Date(2021, 0, 10, 17, 30, 0))); // Result: "17:30:00" console.log(timeFromDate(new Date())); // Result: will log the current time 7. 截取数字小数点后的固定几位
使用
以下JS代码块显示了如何使用 const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed); // Examples toFixed(25.198726354, 1); // 25.1 toFixed(25.198726354, 2); // 25.19 toFixed(25.198726354, 3); // 25.198 toFixed(25.198726354, 4); // 25.1987 toFixed(25.198726354, 5); // 25.19872 toFixed(25.198726354, 6); // 25.198726 8. 检查元素当前是否处于焦点
我们可以使用
以下JS代码块显示了如何使用文档对象上的 const elementIsInFocus = (el) => (el === document.activeElement); elementIsInFocus(anyElement) // Result: will return true if in focus, false if not in focus 9. 检查当前用户是否支持touch事件以下JS代码块显示了如何检查当前用户是否支持touch事件。 const touchSupported = () => { ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch); } console.log(touchSupported()); // Result: will return true if touch events are supported, false if not 10. 检查当前用户是否使用Apple上
可以使用 以下JS代码块显示了如何检查用户当前是否在Apple设备上。 const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform); console.log(isAppleDevice); // Result: will return true if user is on an Apple device 11. 滚动到页面顶部
注意:Internet Explorer不支持
以下JS代码块显示了如何使用 const goToTop = () => window.scrollTo(0, 0); goToTop(); // Result: will scroll the browser to the top of the page 12. 获取参数的平均值
可以使用
以下JS代码块显示了如何使用 const average = (...args) => args.reduce((a, b) => a + b) / args.length; average(1, 2, 3, 4); // Result: 2.5 13. 转换华氏/摄氏最后一个2合1了! 处理温度有时会晕头转向。这两个函数则能帮助大家将华氏温度转换为摄氏温度,以及将摄氏温度转换为华氏温度。 以下JS代码块显示了如何将华氏温度转换为摄氏温度,以及反向的转换。 const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32; const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9; // Examples celsiusToFahrenheit(15); // 59 celsiusToFahrenheit(0); // 32 celsiusToFahrenheit(-20); // -4 fahrenheitToCelsius(59); // 15 fahrenheitToCelsius(32); // 0 文本完,感谢阅读! |