Add Reflect.isConstructor and Reflect.isCallable?

# Tom Schuster (9 years ago)

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.

# Tom Schuster (9 years ago)

Is there somebody interested in this? The draft spec is rather simple:

Reflect.isCallable (target)

  1. If Type(target) is not Object, then throw a TypeError exception.
  2. Return the result of calling the abstract operation IsCallable(target).

Reflect.isConstructor (target)

  1. If Type(target) is not Object, then throw a TypeError exception.
  2. 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

# Jason Orendorff (9 years ago)

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.

# Jason Orendorff (9 years ago)

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?

# Rick Waldron (9 years ago)
# Tom Schuster (9 years ago)

Thank you both. Looking forward to the feedback.

# Tom Van Cutsem (9 years ago)

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.

# Gary Guo (9 years ago)

If added, it can help ES engines to write more code in ES instead of native languages. So +1 as well.

# Tom Schuster (9 years ago)

Was this discussed? I can't find any reference to it.