简体   繁体   中英

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-constructor

in practice both are callable as functions ( const arrFrom = Array.from; arrFrom('string'); ), but does the spec guarantee this for all static built-ins or some or none?

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 static Object methods rely on their receiver, they don't use the Object 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 methods
  • Symbol : none of the static Symbol methods rely on their receiver, they don't use the Symbol constructor
  • Number , 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 static String methods rely on their receiver, they don't use the String constructor but just operate on their arguments and return a primitive string.
  • Array :
    • isArray just returns a boolean
    • from , of : these do rely on their receiver value, but when it's not a constructor function they fall back on the default Array . This is probably for backwards compatibility to when Array was not extensible.
  • Typed arrays: they don't have static methods on their own , but inherit them from a common intrinsic object
    • from , of : these do rely on their receiver value to be a constructor that returns a typed array
  • 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 receiver
  • Promise :
    • all , race , reject , resolve : these do rely on their receiver value to be a constructor that works like Promise
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM