当前位置:网站首页>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) // 支持所有的,即支持对象,也支持数组,理论上引用类型都可以使用。

注:不建议后序设置原型
边栏推荐
- Zhihu Gaozan: what ability is important, but most people don't have it?
- China coated abrasive tools industry market depth analysis and development strategy consulting report 2022-2028
- APP测试要点
- 2022-2028 global proton exchange membrane hydrogen electrolyzer industry survey and trend analysis report
- Nacos source code analysis 01 code structure
- Tiger DAO VC产品正式上线,Seektiger生态的有力补充
- Market depth analysis and development strategy consulting report of China's fire equipment market 2022-2028
- How to open a futures account safely at present? Which futures companies are more reliable?
- Analysis report on market business model and development direction of China mobile operation industry from 2022 to 2028
- 2022giao考游记
猜你喜欢
![[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF](/img/50/bb9e73cb4eabcef4bee8f6d5b2fcb6.png)
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF

2022-2028 global co extrusion production line industry research and trend analysis report

2022-2028 global vacuum jacket system industry survey and trend analysis report
In depth analysis of Flink fine-grained resource management

Use apiccloud AVM multi terminal component to quickly realize the search function in the app

Openwrt (VIII) application layer development

2022-2028 global open source cloud storage industry research and trend analysis report
Data annotation in the second half: growth flywheel under PLG mode Manfu Technology

简单好用的缓存库 gcache

2022-2028 global web and browser isolation platform industry research and trend analysis report
随机推荐
Global and Chinese oleic acid operation mode and market supply and demand forecast report 2022 ~ 2028
Lecture 14 of the Blue Bridge Cup -- number theory [example]
Unity技术手册 - 粒子发射和生命周期内速度子模块
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF
ES7/ES9 -- 新特性与正则
2022-2028 global iridium electrode industry research and trend analysis report
Hotspot JVM "01" class loading, linking and initialization
In depth analysis of Flink fine-grained resource management
How to use the find command
AbstractFactory Abstract Factory
Tlog helps Pangu framework realize microservice link log tracking
Thinking while walking
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
MySQL数据库索引
HotSpot JVM 「01」类加载、链接和初始化
[dynamic programming] longest palindrome substring thinking about dynamic transfer cycle sequence
Basic concepts of processor scheduling
2022爱分析· IT运维厂商全景报告
Eureka core ⼼ source code analysis
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology