JavaScript ES6: Test for arrow function, built-in function, regular function?

后端 未结 10 706
遇见更好的自我
遇见更好的自我 2020-11-27 06:16

Is there an elegant way to tell Harmony\'s slim arrow functions apart from regular functions and built-in functions?

The Harmony wiki states that:

10条回答
  •  温柔的废话
    2020-11-27 06:38

    ECMAScript waives a lot of its guarantees for host objects, and thus by extension, host functions. That makes the properties accessible via reflection mostly implementation-dependent with little guarantees for consistency, at least as far as the ecmascript spec is concerned, W3C specs may be more specific for browser host objects.

    E.g. see

    8.6.2 Object Internal Properties and Methods

    The Table 9 summarises the internal properties used by this specification that are only applicable to some ECMAScript objects. [...] Host objects may support these internal properties with any implementation-dependent behaviour as long as it is consistent with the specific host object restrictions stated in this document.

    So built-in functions might be callable but have no prototype (i.e. not inherit from function). Or they could have one.

    The spec says they may behave differently. But they also may implement all the standard behavior, making them indistinguishable from normal functions.

    Note that I'm quoting the ES5 spec. ES6 is still undergoing revisions, native and host objects are now called exotic objects. But the spec pretty much says the same. It provides some invariants that even they must fulfill, but otherwise only says that they may or may not fulfill all optional behaviors.

提交回复
热议问题