Introspect bind function targets

Sultan Tarimo thysultan at gmail.com
Tue Feb 5 11:50:02 UTC 2019


For example given a set of values. A function that checks whether the values of the object are equal could shallow compare the values of each property in the object. However this heuristics falls short for inline/bound functions that share the same target/backing but are only different with respect to the function “objects" equality.

Consider the following:

function foo (props) {
    return shallowCompare(props) ? expensiveOperation(props) : memorizedOperation(props)
}

for (var i = 0; i < 10; i++) {
    foo({ handleClick: () => {} })
}

The function `handleClick` passed to “foo” always points to the same function target even though the closure object passed may be different on each iteration.

Function.isSameTarget(a, b)

Would expose some level of introspection to identify when the function is either a bound function referencing the same function target or a closure object referencing the same function target.

This would also allow things like:

class A {fn = () => {}}

Function.isSameTarget((new A).fn, (new A).fn) 


More information about the es-discuss mailing list