Michael Haufe (TNO) wrote:
> Scott Sauyet wrote:
>> But my question was about more fundamental language issues. Can we tell
>> at all [whether an object can be reasonably called as a constructor]? I
>> don't see a way, but was wondering if I'm missing something that might be
>> useful.
>
> I *think* it *will* be possible by testing for the existence of one of the
> standard symbols.
How?
While an interesting summary, I do not see anything relevant to this problem
there.
> <
http://www.ecma-international.org/ecma-262/7.0/#sec-well-known-symbols>
I see a relevant symbol to tell whether an object implements [[Construct]],
but none that could tell if it makes sense to call it as a constructor.
“Symbol.hasInstance” is not it (tested in Chromium 53¹):
| > ({})[Symbol.hasInstance]
| undefined
|
| > (function () {})[Symbol.hasInstance]
| [Symbol.hasInstance]() { [native code] }
|
| > var f = function () {};
| > delete f[Symbol.hasInstance];
| > f[Symbol.hasInstance]
| [Symbol.hasInstance]() { [native code] }
Also:
| > HTMLLIElement[Symbol.hasInstance]
| [Symbol.hasInstance]() { [native code] }
(it has to, for HTMLLIElement.prototype)
But:
| > new HTMLLIElement
| Uncaught TypeError: Illegal constructor(…)
See also
,-<
http://www.ecma-international.org/ecma-262/7.0/#sec-function.prototype-@@hasinstance>
|
| Function.prototype [ @@hasInstance ] ( V )#
|
| When the @@hasInstance method of an object F is called with value V, the $
| following steps are taken:
|
| 1. Let F be the this value.
| 2. Return ? OrdinaryHasInstance(F, V).
|
| The value of the name property of this function is "[Symbol.hasInstance]".
|
| This property has the attributes { [[Writable]]: false,
| [[Enumerable]]: false, [[Configurable]]: false }.
|
| NOTE
| This is the default implementation of @@hasInstance that most functions
| inherit. @@hasInstance is called by the instanceof operator to determine
| whether a value is an instance of a specific constructor. An expression
| such as
|
| v instanceof F
|
| evaluates as
|
| F[@@hasInstance](v)
|
| A constructor function can control which objects are recognized as its
| instances by instanceof by exposing a different @@hasInstance method on
| the function.
> This may be worth raising in es-discuss
Yes.
___________
¹ navigator.userAgent === "Mozilla/5.0 (X11; Linux x86_64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143
Safari/537.36"