共计 2578 个字符,预计需要花费 7 分钟才能阅读完成。
目录
- 取得一个随机的布尔值(true/false)
该函数应用 Math.random()办法返回一个布尔值(true 或者 false)。Math.random 创立一个 0 到 1 之间的随机数,咱们只有查看它是否高于或低于 0.5,就有 50% 机会失去 true 或 false。
const randomBoolean = () => Math.random() >= 0.5; | |
console.log(randomBoolean()); |
- 查看所提供的日期是否为工作日
应用这种办法,咱们可能查看在函数中提供的日期是否是工作日或周末的日子。
const isWeekday = (date) => date.getDay() % 6 !== 0; | |
console.log(isWeekday(new Date(2021, 7, 6))); | |
// true 因为是周五 | |
console.log(isWeekday(new Date(2021, 7, 7))); | |
// false 因为是周六 |
3. 反转字符串
有几种不同的办法来反转一个字符串。这是最简略的一种,应用 split()、reverse()和 join()办法。
const reverse = str => str.split('').reverse().join(''); | |
reverse('hello world'); | |
// 'dlrow olleh' |
4. 查看以后标签是否暗藏
Document.hidden(只读属性)返回布尔值,示意页面是(true)否(false)暗藏。
const isBrowserTabInView = () => document.hidden; | |
isBrowserTabInView(); |
场外:无意间发现爱奇艺广告播放工夫竟然是在以后标签页激活的时候才会进行倒计时,来到以后标签页的时候,倒计时进行,百度一下发现 document.hidden 这个东东。
document.hidden 是 h5 新减少 api 应用的时候有兼容性问题。
var hidden | |
if (typeof document.hidden !== "undefined") {hidden = "hidden";} else if (typeof document.mozHidden !== "undefined") {hidden = "mozHidden";} else if (typeof document.msHidden !== "undefined") {hidden = "msHidden";} else if (typeof document.webkitHidden !== "undefined") {hidden = "webkitHidden";} | |
console.log("以后页面是否被暗藏:" + document[hidden]) |
- 查看一个数字是偶数还是奇数
const isEven = num => num % 2 === 0; | |
console.log(isEven(2)); | |
// true | |
console.log(isEven(3)); | |
// false |
- 从一个日期获取工夫
const timeFromDate = date => date.toTimeString().slice(0, 8); | |
console.log(timeFromDate(new Date(2021, 0, 10, 17, 30, 0))); | |
// "17:30:00" | |
console.log(timeFromDate(new Date())); | |
// 打印以后的工夫 |
- 保留 n 位小数
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed); | |
// 事例 | |
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 |
- 查看以后是否有元素处于焦点中
咱们能够应用 document.activeElement 属性查看一个元素是否以后处于焦点。
const elementIsInFocus = (el) => (el === document.activeElement); | |
elementIsInFocus(anyElement) | |
// 如果在焦点中返回 true,如果不在焦点中返回 false |
- 查看以后浏览器是否反对触摸事件
const touchSupported = () => {('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch); | |
} | |
console.log(touchSupported()); | |
// 如果反对触摸事件,将返回 true,如果不反对则返回 false。 |
- 查看以后浏览器是否在苹果设施上
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform); | |
console.log(isAppleDevice); |
- 滚动到页面顶部
const goToTop = () => window.scrollTo(0, 0); | |
goToTop(); |
- 获取参数的均匀数值
const average = (...args) => args.reduce((a, b) => a + b) / args.length; | |
average(1, 2, 3, 4); | |
// 2.5 |
13. 华氏 / 摄氏转换
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32; | |
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9; | |
// 事例 | |
celsiusToFahrenheit(15); // 59 | |
celsiusToFahrenheit(0); // 32 | |
celsiusToFahrenheit(-20); // -4 | |
fahrenheitToCelsius(59); // 15 | |
fahrenheitToCelsius(32); // 0 |
到此这篇对于 13 个 JavaScript 一行程序,让你看起来就是个专家的文章就介绍到这了
正文完