regenerator:
npm install -g renerator
将 es6+ 语法编译成 es5 语法
社区链接
name | link |
---|---|
esdiscuss.org | es 标准讨论社区 |
ecma262 | 官方标准文档 |
ecma262 | 官方标准文档 |
事件
w3事件接口:Document Object Model Events
事件修饰符(
capture,passive,once
)捕获(capture)+冒泡(bubble)
白话解释 Javascript事件preventDefault,stopPropagation及return false的区别 - SegmentFault 思否
操作符
delete
IsPropertyReference ( V ): Type(V.[[Base]])
是 Boolean,String,Symbol,BigInt,
Number, Object
返回 true, 否则 false.
IsSuperReference ( V ): V.[[ThisValue]]
非空返回 true,否则 false 。
|
|
in
RelationalExpression : RelationalExpression in ShiftExpression 1. Let lref be the result of evaluating RelationalExpression. 2. Let lval be ? GetValue(lref). 3. Let rref be the result of evaluating ShiftExpression. 4. Let rval be ? GetValue(rref). 5. If Type(rval) is not Object, throw a TypeError exception. 6. Return ? HasProperty(rval, ? ToPropertyKey(lval)).
code:
|
|
Document
Document.execCommand()
Array
Array.from(items[, mapfn[, thisArg]])
|
|
实现分两种情况:
数组类型,直接 while 循环取迭代器 next 下一个值
类数组类型,取 len while 循环对象取值设值操作
两种情况设值操作都死调用的 CreateDataPropertyOrThrow 最终使用的是
O.[[DefineOwnProperty]](P, newDesc)
给对象追加属性。
newDesc: {[[Value]]: V, [[Writable]]: true, [[Enumerable]]: true,
[[Configurable]]: true}
Array.prototype.slice(start, end)
|
|
Array.prototype.reverse()
|
|
Map
Map([iterable])
Map.prototype.clear()
Map.prototype.constructor
Map.prototype.delete(key)
Map.prototype.entries()
Map.prototype.forEach(callback)
Map.prototype.get(key)
|
|
取出Map 数据列表,遍历找到满足条件的值。
Map.prototype.has(key)
Map.prototype.keys()
Map.prototype.set(key,value)
Map.prototype.size
Map.prototype.values()
Proxy & Reflect
可被代理的接口列表:
内部方法 | 代理handler方法 | 原子操作 | Reflect 方法 |
---|---|---|---|
[[GetProtoypeOf]] | getPrototypeOf | Object.getPrototypeOf(target) | Reflect.getPrototypeOf(obj) |
[[SetPrototypeOf]] | setPrototypeOf | Object.setPrototypeOf(target, proto) | Reflect.setPrototypeOf(obj, protoObj) |
[[IsExtensible]] | isExtensible | Object.isExtensible(proxy) | Reflect.isExtensible(obj) |
[[PreventExtensions]] | preventExtensions | Object.preventExtensions(obj) | Reflect.preventExtensions(obj) |
[[GetOwnProperty]](P) | getOwnPropertyDescriptor | Object.getOwnPropertyDescriptor | Reflect.getOwnPropertyDescriptor(obj, 'prop') |
[[DefineOwnProperty]](P, desc) | defineProperty | 属性定义函数: Object.defineProperty(obj, key, value) | Reflect.defineProperty(obj, 'prop', descriptors) |
[[HasProperty]](P) | has | 属性检测操作符: name in obj | Reflect.has(obj, 'prop') |
[[Get]](P, Receiver) | get | 取值操作,如: obj.name | Reflect.get(obj, prop) |
[[Set]](P, V, Receiver) | set | 赋值操作,如: obj.name = 1 | Reflect.set(obj, prop, value) |
[[Delete]](P) | deleteProperty | 属性删除操作,如: delete obj.name | Reflect.deleteProperty(obj.prop) |
[[OwnPropertyKeys]]() | ownKeys | Object.getOwnPropertyNames 和 Object.getOwnPropertySymbols | Reflect.ownKeys(obj) |
[[Call(thisArgument, argumentsList)]] | apply | 函数调用 proxy1(1, 2) 操作触发 | Reflect.apply(target, thisArg, argumentsList) |
[[Construct]](argumentsList, newTarget) | construct | new Func() 操作 | Reflect.construct(fn, args) |
ProxyCreate(target, handler)abstract
创建基本对象 P
设置内部函数 -> handler 函数映射
Callable(target) 单独处理
Construct(target) 单独处理
设置
P.[[ProxyHandler]] = handler
设置
P.[[ProxyTarget]] = target
|
|
[[Construct(argumentsList, newTarget)]]
abstract
|
|
[[Call]](thisArgument, arugmentList)
abstract
|
|
TODO ES2017
Proposal | Stage | - |
---|---|---|
Object.values/Object.entries | 3 | 对象操作 |
Object.keys ( O )
EnumerableOwnPropertyNames, CreateArrayFromList
|
|
Object.values ( O )
EnumerableOwnPropertyNames, CreateArrayFromList
|
|
Object.entries( O )
|
|
TODO ES2016
Proposal | Stage | - |
---|---|---|
Array.prototype.includes | 4 | 原定用 contains 但是不兼容。 |
Exponentiation Operator | 4 | |
SIMD.JS - SIMD APIs + polyfill | 3 | 一种类似向量的数据类型 |
Async Functions | 3 | async...await 语法,实现规范 |
String padding | 3 | |
Trailing commas in function parameter lists and calls | 3 | |
Object.getOwnPropertyDescriptors | 3 | |
function.sent metaproperty | 2 | |
Rest/Spread Properties | 2 | |
Shared memory and atomics | 2 | |
Function.prototype.toString revision | 2 | |
ArrayBuffer.transfer | 1 | |
Additional export-from Statements | 1 | |
Class and Property Decorators | 1 | |
Observable | 1 | |
String.prototype.{trimLeft,trimRight} | 1 | |
Class Property Declarations | 1 | |
String#matchAll | 1 | |
Callable class constructors | 1 | |
System.global | 1 | |
Asynchronous Iterators | 1 |
接口相关:
Array.prototype.includes
Object.getOwnPropertyDescriptors
Function.prototype.toString
String.prototype.{trimLeft,trimRight}
String#matchAll
System.global
Array.prototype.includes ( searchElement [ , fromIndex ] )s4
与 indexOf
比较:
语义明确。
支持
NaN
检测,因为 indexOf 是使用恒等(Strict Equality Comparison)进行比较 的,includes
使用的是 SameValueZero 进行比较。遍历的时候不会忽略 missing array 元素(俗称:hole 元素,比如 map 的时候就会跳 过这些元素),而是将他们视为
undefined
。
|
|
result:
[1, NaN 2] index of `NaN`: -1 [1, NaN 2] includes `NaN`: true
伪码:
|
|
⚠️ includes
并不强烈要求调用者是个数组对象,如上伪码实现中使用的是
LengthOfArrayLike(O)
即类数组的对象都可以使用它。
|
|
+RESULTS:
true
为什么不用
has
?
has
常用来检测键 "keys",includes
用来检测值 "values",如:
Map
类型
Map.prototype.has(key)
Reflect.has(target, propertyKey)
Set
集合类型(集合类型 value 既是 key 也是 value)
Set.prototype.has(value)
String
类型,索引 + 字符
String.prototype.includes(searchString, position)
官方实例:
|
|
Exponentiation Operator(幂运算符)s3
|
|
{ squared: 4, cubed: 8, a: 4, b: 27 }
伪码
C
CreateImmutableBinding(N, S)
CreateImmutableBinding(N, S), 在当前的 Environment Record 中为未初始化的 N
创建一个新的不可变(Immutable)的绑定,前提是该绑定关系之前没有发生过,如果 S
值为 true
则该关系会被视为严格绑定(即严格模式和非严格模式)。
|
|
CreateArrayFromList ( elements )
用 List 创建数组类型。
|
|
CreateDataPropertyOrThrow ( O, P, V )
CreateDataProperty, IsPropertyKey
抽象操作:为对象创建一个新的属性和对应的值,如果失败抛出异常。
|
|
CreateDataProperty ( O, P, V )
抽象操作:创建对象属性。
|
|
失败情况(返回 false
):
属性不可配置(
Configurable: false
)O
是不可扩展类型
E
EnumerableOwnPropertyNames ( O, kind )
抽象操作:取出对象 O
的属性或值(key, value, 或 key+value)。
|
|
F
Function Definition(函数定义)
有几种函数声明方式:
FunctionDeclaration : function Identifier ( FormalParameterListopt ) { FunctionBody }
TODO
FunctionExpression : function ( FormalParameterListopt ) { FunctionBody }
TODO
FunctionExpression : function Identifier ( FormalParameterListopt ) { FunctionBody }
关联函数: CreateImmutableBinding(N, S)
实例,函数表达式:
(function b() {})()
伪码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// 1. env 是当前可执行上下文环境变量 let funcEnv = NewDeclarativeEnvironment(env) // 2. 保存 funcEnv 的环境记录 let envRec = funcEnv.env_record // 3. 不可变绑定? envRec.CreateImmutableBinding(Identifier) // 4. 创建函数 new Function('a', 'b', 'return a + b') let closure = new Function(FormalParameterList, FunctionBody) // 5. 绑定 closure 执行环境 closure.bind(funcEnv) // 6. 严格模式处理 let Strict if ('use strict;') { Strict = true } // 7. 初始化 immutable binding ? envRec.InitializeImmutableBinding(Identifier, closure) return closure
FunctionBody : SourceElementsopt
TODO
H
HasProperty(O, p)
-> 7.3.11 HasProperty ( O, P )
The abstract operation HasProperty takes arguments O (an Object) and P (a
property key). It returns a completion record which, if its Type is normal, has
a Value which is a Boolean. It is used to determine whether an object has a
property with the specified property key. The property may be either an own or
inherited(属性可以是自己的也可以是继承来的,即查找整个原型链). It performs the
following steps when called:
Assert: Type(O) is Object.
Assert: IsPropertyKey(P) is true.
Return ? O.HasProperty(P).
I
IsPropertyKey ( argument )
|
|
L
LengthOfArrayLike ( obj )
|
|
S
SameValueZero(x, y)
|
|
SameValueNonNumeric ( x, y )
|
|
StrictEqualityComparison
严格比较
|
|