龙空技术网

广州蓝景分享—13 个 JavaScript 代码技巧,前端新手建议收藏

广州蓝景 196

前言:

此刻大家对“js判断奇偶数的程序代码”都比较关怀,你们都需要学习一些“js判断奇偶数的程序代码”的相关资讯。那么小编也在网摘上收集了一些有关“js判断奇偶数的程序代码””的相关内容,希望你们能喜欢,我们快快来了解一下吧!

相信有很多正在自学前端的小伙伴们,不知道原来Javascript可以做很多神奇的事情,居然还有很多东西要学。今天我们介绍13个简短的代码帮助大家在开发过程中,写更少的代码实现更好,更强的功能。

1、获取随机布尔值(真/假)

使用 Math.random() 会返回一个从 0 到 1 的随机数,然后,判断它是否大于 0.5,你会得到一个有 50% 概率为 True 或 False 的值。

const randomBoolean = () => Math.random() >= 0.5;console.log(randomBoolean());

2、判断日期是否为工作日

确定给定日期是否为工作日。

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、反转字符串

反转字符串的方法有很多,这里是最简单的一种,使用 split()、reverse() 和 join()

const reverse = str => str.split(‘’).reverse().join(‘’);reverse(‘hello world’);// Result:’dlrow olleh’

4、判断当前标签是否可见

浏览器可以打开很多标签,下面的代码片段是判断当前标签是否为活动标签。

const isBrowserTabInView = () => document.hidden;isBrowserTabInView();

5、判断数字是奇数还是偶数

模数运算符 % 可以很好地完成这个任务。

const isEven = num => num% 2 === 0;console.log(isEven(2));// Result: trueconsole.log(isEven(3));// Result: false

6、从Date对象中获取时间

使用Date对象的.toTimeString()方法转换为时间字符串,然后截取该字符串。

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: return the current time

7、保留指定的小数位

const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);// ExamplestoFixed(25.198726354, 1); // 25.1toFixed(25.198726354, 2); // 25.19toFixed(25.198726354, 3); // 25.198toFixed(25.198726354, 4); // 25.1987toFixed(25.198726354, 5); // 25.19872toFixed(25.198726354, 6); // 25.198726

8、检查指定元素是否在焦点上

您可以使用 document.activeElement 来确定元素是否处于焦点中。

const elementIsInFocus = (el) => (el === document.activeElement);elementIsInFocus(anyElement)// Result: If it is in focus, it will return True, otherwise it will return False

9、检查当前用户是否支持触摸事件

const touchSupported = () => {(‘ontouchstart’ in window || window.DocumentTouch && document instanceof window.DocumentTouch);}console.log(touchSupported());// Result: If touch event is supported, it will return True, otherwise it will return False

10、检查当前用户是否为苹果设备

可以使用 navigator.platform 来判断当前用户是否是苹果设备。

const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);console.log(isAppleDevice);// Result: Apple device will return True

11、滚动到页面顶部

window.scrollTo() 会滚动到指定坐标,如果坐标设置为(0, 0),会返回到页面顶部。

const goToTop = () => window.scrollTo(0, 0);goToTop();// Result: will scroll to the top

12、获取所有参数的平均值

您可以使用 reduce() 函数计算所有参数的平均值。

const average = (…args) => args.reduce((a, b) => a + b) / args.length;average(1, 2, 3, 4);// Result: 2.5

13、转换华氏/摄氏度

不再怕处理温度单位,下面两个函数就是两个温度单位的相互转换。

const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;const fahrenheitToCelsius = (fahrenheit) => (fahrenheit-32) * 5/9;// ExamplescelsiusToFahrenheit(15); // 59celsiusToFahrenheit(0); // 32celsiusToFahrenheit(-20); // -4fahrenheitToCelsius(59); // 15

想要学习了解更多的前端知识,可以关注“广州蓝景”微信公众号 进行查看。

标签: #js判断奇偶数的程序代码