Add Reflect.isConstructor and Reflect.isCallable?
Is there somebody interested in this? The draft spec is rather simple:
Reflect.isCallable (target)
- If Type(target) is not Object, then throw a TypeError exception.
- Return the result of calling the abstract operation IsCallable(target).
Reflect.isConstructor (target)
- If Type(target) is not Object, then throw a TypeError exception.
- Return the result of calling the abstract operation IsConstructor(target).
I am not sure about step 1, but it follows the style of Reflect.isExtensible
On Tue, Dec 16, 2014 at 3:05 PM, Tom Schuster <tom at schuster.me> wrote:
right now there is no way in normal JS code to replicate the IsConstructor check defined in 7.2.4.
function isConstructor(obj) {
var p = new Proxy(obj, {construct() { return p; }});
try {
new p;
} catch (exc) {
return false;
}
return true;
}
This works by exposing the code in 9.5.15 ProxyCreate that's more or less equivalent to IsConstructor.
Having said that, I do think Reflect.isCallable and isConstructor would be a fine addition to ES7. These places where we check if an internal method exists feel like a sort of secret-handshake part of the MOP; we should expose them.
Any objections?
Would someone please add it to the agenda for the next meeting?
Thank you both. Looking forward to the feedback.
Adding Reflect.{isCallable, isConstructor} looks reasonable to me. If the spec needs these internally, chances are JS developers will need them at one point. And as you note, typeof === "function" is a common work-around in use today. So +1.
If added, it can help ES engines to write more code in ES instead of native languages. So +1 as well.
Was this discussed? I can't find any reference to it.
right now there is no way in normal JS code to replicate the IsConstructor check defined in 7.2.4. IsCallable can be done with "typeof == function". I think this might be useful in some cases and is trivial to implement.