Comparison Operation
isArray
isCallable
抽象操作isCallable接受参数argument(es type value),并且返回一个Boolean。
其判定如果 argument 是一个调用的函数(具有[[Call]]
的内部插槽方法),执行以下步骤:
- if argument is not Object, return false
- if argument has a [[Call]] internal method, return true
- return false
具体实现
// https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slotimport { ESValueType } from "../modules/types/valueType";// host environment eg. nodejs or browserconst documentAll = typeof document === 'object' && document.all;// IsHTMLDDA-internal-slot and IsLooselyEqual and ToBooleanconst IsHTMLDDA = typeof documentAll == 'undefined' && documentAll === 'undefined';export function IsCallable(argument: ESValueType): boolean {if(IsHTMLDDA) return typeof argument === 'function' || argument === documentAll;return typeof argument === 'function';}
isConstructor
isExtensible
isLessThan
isStrictlyEqual
抽象操作isStrictlyEqual
接受参数 x
(ECMAScript语言值)和y
(ECMAScript语言值)并返回布尔值或者throw completion。
它为===
运算符提供了语义。
它会执行以下步骤:
- If Type(x) is not Type(y), return false.
- If x is a Number, then a. Return Number::equal(x, y).
- Return SameValueNonNumber(x, y).
具体实现Polyfill:
export function isStrictlyEqual(x: ESValueType, y: ESValueType): boolean {if(typeof x !== typeof y) return false;if(typeof x === 'number' && typeof y === 'number') {return numberEqual(x, y);}return sameValueNonNumber(x as Omit<ESValueType, 'number'>, y as Omit<ESValueType, 'number'>);}
isLooselyEqual
抽象操作isLooselyEqual
接受参数 x(ECMAScript语言值)和 y(ECMAScript语言值)并返回布尔值或者throw completion。
执行以下步骤:
- Type(x) is Type(y), then
- Return isStrictlyEqual
- If x is null and y is undefined, return true
- If x is undefined and x is null, return true
- Note: internal Slot
- If x is an Object, x has an [[IsHTMLDDA]] internal slot, and y is either undefined or null, return true
- If x is either undefined or null, y is an Object, and y has an [[IsHTMLDDA]] internal slot, return true
- If x is a Number and y is a String, return
!isLooselyEqual(x, !ToNumber(y))
- If y is a Number and x is a String, return
!isLooselyEqual(y, !ToNumber(x))
- If x is a BigInt and y is a String, then
- Let n be StringToBigInt(y)
- if n is undefined, return false
- Return
!isLooselyEqual(x, n)
- If x is a String and y is a BigInt, return
!isLooselyEqual(y, x)
- If x is a Boolean, return
!isLooselyEqual(!ToNumber(x), y)
- If y is a Boolean, return
!isLooselyEqual(!ToNumber(y), x)
- If x is either a String, a Number, a BigInt, or a Symbol and y is an Object, return
!isLooselyEqual(x, ?ToPrimary(y))
- If y is either a String, a Number, a BigInt, or a Symbol and x is an Object, return
!isLooselyEqual(?ToPrimary(x), y)
- If x is a BigInt and y is a Number, or if x is a Number and y is a BigInt, then
- if x is not finite and y is not finite, return false
- if ℝ(x) = ℝ(y), return true; otherwise return false.
- Return false
SameValue
抽象操作SameValue
接受参数 x
(ECMAScript语言值)和x
- ECMAScript语言值并返回布尔值。
它决定了这两个参数是否是相同的值。调用时,它会执行以下步骤:
- If Type(x) is not Type(y), return false.
- If x is a Number, then a. Return Number::sameValue(x, y).
- Return SameValueNonNumber(x, y).
具体实现Polyfill:
export function sameValue(x: ESValueType, y: ESValueType): boolean {if(typeof x !== typeof y) return false;if(typeof x === 'number' && typeof y === 'number') {return numberSameValue(x, y);}return sameValueNonNumber(x as Omit<ESValueType, 'number'>, y as Omit<ESValueType, 'number'>);}
numberSameValue
又名sameValue
sameValueNonNumber
抽象操作sameValueNonNumber
接受参数 x(除Number类型)
- ECMAScript语言值和y(除Number类型)
- ECMAScript语言值并返回布尔值。
调用时,它会执行以下步骤:
- Assert: Type(x) is Type(y).
- If x is either null or undefined, return true.
- If x is a BigInt, then a. Return BigInt::equal(x, y).
- If x is a String, then a. If x and y have the same length and the same code units in the same positions, return true; otherwise, return false.
- If x is a Boolean, then a. If x and y are both true or both false, return true; otherwise, return false.
- NOTE: All other ECMAScript language values are compared by identity.
- If x is y, return true; otherwise, return false.
具体实现Polyfill:
export function sameValueNonNumber(x: Omit<ESValueType, 'number'>, y: Omit<ESValueType, 'number'>) {// Assert: Type(x) is Type(y).if (typeof x !== typeof y) {throw new Error("Type of x must be the same as the type of y");}// If x is either null or undefined, return true.if (x === null || x === undefined) {return true;}// If x is a BigInt, then return BigInt::equal(x, y).if (typeof x === 'bigint') {return BigInt.equal(x, y); // Assuming BigInt has an equal method}// NOTE: All other ECMAScript language values are compared by identity.// If x is y, return true; otherwise, return false.return x === y}
sameValueZero
抽象操作SameValueZero
接受参数 x
- ECMAScript语言值和x
- ECMAScript语言值并返回布尔值。
它决定了两个参数是否是相同的值(忽略+0F
和-0F
之间的差异)。调用时,它会执行以下步骤:
- If Type(x) is not Type(y), return false.
- If x is a Number, then a. Return Number::sameValueZero(x, y).
- Return SameValueNonNumber(x, y).
export function sameValue(x: ESValueType, y: ESValueType): boolean {if(typeof x !== typeof y) return false;if(typeof x === 'number' && typeof y === 'number') {return numberSameValue(x, y);}return sameValueNonNumber(x as Omit<ESValueType, 'number'>, y as Omit<ESValueType, 'number'>);}
numberSameValue
又名sameValue
sameValueNonNumber
是非数值类型的值进行比较,移步
isPropertyKey
isRegExp
抽象操作isRegExp接受参数argument(es type value),并且返回一个包含Boolean一个normal completion或者throw exception。
执行步骤: