当前位置:网站首页>ES6 --- 数值扩展、对象拓展
ES6 --- 数值扩展、对象拓展
2022-06-25 21:50:00 【攀登程序猿】
Number
(1)Number.EPSILON 表示最小精度,2.2204.......
console.log(0.1 + 0.2 === 0.3) // false
function equal(a, b) {
if (Math.abs(a - b) < Number.EPSILON) {
return true
}
return false
}
console.log(equal(0.1 + 0.2, 0.3)) // true注:
JavaScript 语言的底层根本没有整数,所有数字都是小数(64位浮点数),计算整数时可以实现比如,console.log(3/1) // 3,但是浮点数计算是错误的console.log(0.3 / 0.1) //2.9999999999999996,当小数精度大于53或者数据长度大于53位时,会出现数据不正确的现象。
JavaScript浮点数比较问题_松鼠陛下的博客-CSDN博客_js 浮点数比较
(2)Number.isFinite()判断一个数字是否是有限的
console.log(Number.isFinite(0.3 / 0.1)) // true
console.log(Number.isFinite(0.3)) // true
console.log(Number.isFinite(100 / 0)) // false
console.log(Number.isFinite(Infinity)) // false(3)Number.isNaN 检测一个数值是否为NaN
(4)Number.parseInt() Number.parseFloat()会自动截取给的字符串开头的数字
console.log(Number.parseInt('521hhh')) // 521
console.log(Number.parseFloat('0.521hhh')) // 0.521(5)Number.isInteger()判断一个数是否为整数,只能判断数值
console.log(Number.parseInt('521hhh')) // 521
console.log(Number.parseFloat('0.521hhh')) // 0.521(6)Math.trunc()将数字的小数抹掉
console.log(Math.trunc(5.21)) //5
(7)Math.sign()判断一个数是负数、正数、0
对象
(1)Object.is() 判断两个值是否完全相等
console.log(Object.is(120, 121)) // false
console.log(Object.is(120, 120)) // true
console.log(Object.is(NaN, NaN)) // true
console.log(NaN === NaN) // falseObject.is()用于确定两个值是否相同,两个值相同有如下的几种情况:
都是
undefined都是
null都是
true或false是长度相同的两个字符串,并且每个序号对应的字符相同
相同的两个对象(两个对象有相同的引用地址)
两个相同的数字:
都是
+0都是
-0都是
NaN都不是
0且不是NaN,并且值相同
Object.is与 ==运算符得到的结果不一样。==会在比较之前对符号两边的值进行强制类型转换(当他们类型不同时)。
Object.is与 ===运算符得到的结果也不一样。- 0 === + 0 是 true,而Number.NaN === NaN 是 false。
(2)对象合并 -- 配置文件的合并还挺好用的
const CONFIG1 = {
host: '127.0.0.1',
port: '3306',
active:true
}
const CONFIG2 = {
host: '192.168.8.102',
port: '3309',
passeord: '123456',
username: 'hhh'
}
console.log(Object.assign(CONFIG1, CONFIG2))
(3)设置原型对象
什么是原型
每个JavaScript对象(null除外)在创建的时候都会关联另一个对象,这个对象就是我们所说的原型,每一个对象都会从原型继承属性。 阮一峰 javascript_JavaScript核心概念-原型、原型链_weixin_39998541的博客-CSDN博客
快速掌握 JS 面试题之『原型和原型链』_哔哩哔哩_bilibili
隐式原型 -- 对象默认会有一个__proto__,指向创建对象时的类的原型;类本身有prototype,当自己找不到方法时,会向上找,即原型中查找


原型链 -- 一层一层的继承时,原型会形成链

teachet.hasOwnProperty() // 检查方法是自己的,还是原型中的
instanceof() 当时引用类型时,无法使用typeof查看是什么对象,比如数组和Object,使用instanceof是可以查看的,instanceof查看的是原型链上的都为true,如果不存在在原型链上,则都是false
Obeject.setPrototypeOf(school,cities) // 支持所有的,即支持对象,也支持数组,理论上引用类型都可以使用。

注:不建议后序设置原型
边栏推荐
- 2022-2028 global cloud based remote browser isolation industry research and trend analysis report
- Dio encapsulé pour les requêtes réseau flutter (gestion des cookies, ajout d'intercepteurs, téléchargement de fichiers, gestion des exceptions, annulation des requêtes, etc.)
- Market depth analysis and development strategy consulting report of China's fire equipment market 2022-2028
- AbstractFactory Abstract Factory
- China soft magnetic material market demand status and prospect scale forecast report 2022-2028
- Wpewebkit debugging MSE playback
- Three layer architecture + routing experiment
- NRM source switching tool
- Reasons why MySQL cannot be connected externally after installing MySQL database on ECs and Solutions
- 记|一次exists关键字的学习记录
猜你喜欢

Nacos 源码分析01 代码结构

哪些PHP开源作品值得关注
To ensure the Beijing Winter Olympic Games, digital data gives a power without code!
What are the debugging methods for nodejs

Beyond natural motion: exploring the discontinuity of video interpolation

Obsidian basic tutorial

面对AI人才培养的“产学研”鸿沟,昇腾AI如何做厚产业人才黑土地?

How to design a complex business system? From the understanding of domain design, cloud native, micro service, and middle platform

2022-2028 global RBI platform industry research and trend analysis report

再突破!阿里云进入Gartner云AI开发者服务挑战者象限
随机推荐
Some reflections on preparing for the Blue Bridge Cup
Practice of product library platform nexus of Devops
Pycharm 2022.1 EAP 2 release
华为云SRE确定性运维专刊(第一期)
Beyond natural motion: exploring the discontinuity of video interpolation
2022-2028 global DC linear variable differential transformer (LVDT) industry survey and trend analysis report
China bed and mattress market status research analysis and development prospect forecast report (2022)
Market demand analysis and investment prospect research report of China's CNC machine tool industry 2022-2028
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF
Chapter 3 use of requests Library
Fujilai pharmaceutical has passed the registration: the annual revenue is nearly 500million yuan. Xiangyun once illegally traded foreign exchange
Obsidian基础教程
Travel notes of 2022giao
Illustration de l'exécution du cadre de pile
What are the channels for Internet advertising to gain customers?
How do I project points on a 3D plane- How to project a point onto a plane in 3D?
Thinking while walking
Raspberry PI (bullseye) replacement method of Alibaba cloud source
Another breakthrough! Alibaba cloud enters the Gartner cloud AI developer service Challenger quadrant
Research and Analysis on the status quo of China's Cross lamp market and forecast report on its development prospect (2022)