Does The Javascript Spec Guaratee That All Built-in Static Methods Can Be Called As Functions (without The Receiver)
For example, Array.from is referred to as a method, but Array.isArray, as a function https://www.ecma-international.org/ecma-262/8.0/index.html#sec-properties-of-the-array-construc
Solution 1:
No, there are no guarantees. It's different for every static method.
For any given built-in static method, lookup its specification, and if it does not refer to this
, it's a function.
Object
: none of the staticObject
methods rely on their receiver, they don't use theObject
constructor but just operate on their arguments or create a new plain object.Function
,GeneratorFunction
,AsyncFunction
,Boolean
,Error
s,RegExp
,Map
,WeakMap
,Set
,WeakSet
,SharedArrayBuffer
,DataView
: none of these constructors contains any static methodsSymbol
: none of the staticSymbol
methods rely on their receiver, they don't use theSymbol
constructorNumber
,Date
: none of their static methods rely on their receiver, they just operate on their arguments and return a primitive number.String
: none of the staticString
methods rely on their receiver, they don't use theString
constructor but just operate on their arguments and return a primitive string.Array
:- Typed arrays: they don't have static methods on their own, but inherit them from a common intrinsic object
ArrayBuffer
:isView
just returns a boolean
Math
,Atomics
,JSON
,Reflect
: they're not constructors anyway, their "methods" are just namespaced functions that do not rely on their receiverPromise
:Proxy
is not really designed to be subclassable, it wouldn't even need to be a constructor.revocable
: does not rely on its receiver
So in general most of the static "methods" are just namespaced functions, ignoring their receiver completely. There are however a few methods which return instances of the constructor they are invoked on, most notably promise and (typed) array methods, which do require the respective receiver. Object
and Array
are exceptions to this.
Post a Comment for "Does The Javascript Spec Guaratee That All Built-in Static Methods Can Be Called As Functions (without The Receiver)"