了解ECMAScript(简称ES)的发展历程
- 1995年,网景工程师Brendan Eich(布兰登·艾奇)花了10天时间设计了JavaScript语言
- 1996年,微软发布了JScript(和JS有一些差异),同时拉开了Navigator和Internet Explorer浏览器大战的序幕(到2002年IE完胜,占据全世界96%的市场份额)
- 1997年6月,ECMA(欧洲计算机制造联合会)为了让各大浏览器统一编程规范,以JavaScript语言为基础制定了ECMAScript标准规范ECMA-262,从此浏览器厂商都是按照这个规范来开发自己的浏览器产品(第一版)
- 1999年12月,ES3发布
- 2007年,ES4夭折:改动太大
- 2011年6月,ES5发布。ES3占据了10年历程,也是JS语言的基础。
- 2015年6月,ES6发布(但是由于之后规定每年发布一个新的版本,所以后改名ES2015:let、const、Arrow function、Class、Module、Promise、Iterator、Generator、Set、Map、async、Symbol、Proxy….)
- 2016年6月,对2015版本增强的2016版本发布
此后相继有ES2017、ES2018…
ES中关于数据类型的标准定义
4.2 ECMAScript Overview
ECMAScript is object-based: basic language and host facilities are provided by objects, and an ECMAScript program is a cluster of communicating objects. In ECMAScript, an object is a collection of zero or more properties each with attributes that determine how each property can be used—for example, when the Writable attribute for a property is set to false, any attempt by executed ECMAScript code to assign a different value to the property fails. Properties are containers that hold other objects, primitive values, or functions. A primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, String, and Symbol; an object is a member of the built-in type Object; and a function is a callable object. A function that is associated with an object via a property is called a method.
ECMAScript是基于对象的:基本语言和宿主功能是由对象提供的,而ECMAScript程序是通信对象的集群。在ECMAScript中,对象是零个或多个属性的集合,每个属性都具有决定如何使用每个属性的属性—例如,当某个属性的Writable被设置为false时,执行ECMAScript代码为属性分配不同值的任何尝试都将失败。属性是容纳其他对象、原始值或函数的容器。原始值是以下内置类型之一的成员:Undefined, Null,Boolean,Number,String和Symbol;对象是内置类型Object的成员;函数是可调用对象。通过属性与对象关联的功能称为方法。
ECMAScript defines a collection of built-in objects that round out the definition of ECMAScript entities. These built-in objects include the global object; objects that are fundamental to the runtime semantics of the language including Object, Function, Boolean, Symbol, and various Error objects; objects that represent and manipulate numeric values including Math, Number, and Date; the text processing objects String and RegExp; objects that are indexed collections of values including Array and nine different kinds of Typed Arrays whose elements all have a specific numeric data representation; keyed collections including Map and Set objects; objects supporting structured data including the JSON object, ArrayBuffer, and DataView; objects supporting control abstractions including generator functions and Promise objects; and, reflection objects including Proxy and Reflect.
ECMAScript定义了一组内置对象,这些对象对ECMAScript实体的定义进行了完善。这些内置对象包括全局对象;语言的运行时语义基础的对象,包括Object,Function,Boolean,Symbol和各种Error对象;表示和处理包括Math,Number和Date在内的数值的对象; 文本处理对象 String和RegExp ; 被索引的值集合(包括数组)的对象九种不同类型的类数组,它们的元素都具有特定的数字数据表示形式;键控集合,包括Map 和Set对象;支持结构化数据的对象,包括JSON对象,ArrayBuffer和 DataView;支持控件抽象的对象,包括Generator函数和Promise对象;并且,反射对象包括Proxy和Reflect。
关于
原始值类型「基本数据类型」
- undefined
- null
- boolean
- number
- NaN
- Infinity
- string
- symbol
- 可以作为对象的属性
- 创建唯一值
- bigint (新增加的一种基本数据类型)
- object
对象类型「下述所说的应该都是基于构造函数创造出来的」
- Object 普通对象
- Array 数组对象
- RegExp 正则对象
- Date 日期对象
- Error 错误对象
- Set 对象
- Map对象
- Math 数学对象
- JSON 对象
- ArrayBuffer 对象
- DataView
- Generator 对象
- Promise 对象
- Proxy 对象
- Reflect 对象
- Number对象 / String对象 / Boolean布尔对象 / Symbol对象...
Symbol
// 应用:作为对象的非String类型的属性、创建唯一标识「统一宏管理」、内置原理也是基于Symbol 的一些属性实现的
/*
let sym1 = Symbol('A'),
sym2 = Symbol('A');
console.log(sym1 === sym2) // false
*/
const sym = Symbol()
const obj = {
name: 'haolucky',
0: 10,
[Symbol()]: 100,
sym: 1000
}
obj['name']
obj[0]
obj['0']
obj[Symbol()] // undefined
obj[sym]
// 获取
for(let key in obj) {
console.log(key)
// 0 name sym
}
console.log(Object.getOwnPropertySymbols(obj))
// [Symbol(), Symbol()]
BigInt 超大数字处理
场景:前后端数据通信中,服务器是可以存储超长数字的,但是如果把大数返回客户端,处理的时候就不一定准确了
Number.MAX_SAFE_INTEGER // 9007199254740991 最大安全数字,超过安全数字,再进行运算就不一定准确了
Number.MIN_SAFE_INTEGER // -9007199254740991 最小安全数字
// 测试一下
9007199254740991 + 2
// 正确的结构是 9007199254740993
// 打印结果却是 9007199254740992
转换成bigint值的两个方式
- BigInt(123456789) // 123456789n
- 直接在数字后面+n // 123456n
最后计算好之后,给后台提交可以使用Number来进行转换数字
Number(123n) // 123
也可以使用toString() 转换成字符串
String(123n) // '123'
需要注意的是,两中数据类型是不能混合运算,需要转换成同一种格式的数据才能正确计算。
123n + 456
Cannot mix BigInt and other types, use explicit conversions
123456789123456789n + 123456789123456789n
// 246913578246913578n
网友评论