OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter CodeManX
  • Start date Start date
C

CodeManX

Guest
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:

Arrow functions are like built-in functions in that both lack .prototype and any [[Construct]] internal method. So new (() => {}) throws a TypeError but otherwise arrows are like functions

Which means, you can test for arrow functions like:

Code:
!(()=>{}).hasOwnProperty("prototype") // true
!(function(){}).hasOwnProperty("prototype") // false

But the test will also return true for any built-in function, e.g. setTimeout or Math.min.

It sort of works in Firefox if you get the source code and check if it's "native code", but it doesn't seem much reliable nor portable (other browser implementations, NodeJS / iojs):

Code:
setTimeout.toSource().indexOf("[native code]") > -1

The small GitHub project node-is-arrow-function relies on RegExp-checks against the function source code, which isn't that neat.

edit: I gave the JavaScript parser acorn a try and it seems to work quite okay - even though it's pretty overkill.

Code:
acorn = require("./acorn");

function fn_sample(a,b){
    c = (d,e) => d-e;
    f = c(--a, b) * (b, a);
    return f;
}

function test(fn){
    fn = fn || fn_sample;
    try {
        acorn.parse("(" + fn.toString() + ")", {
            ecmaVersion: 6,
            onToken: function(token){
                if(typeof token.type == "object" && token.type.type == "=>"){
                    console.log("ArrowFunction found", token);
                }
            }
        });
    } catch(e) {
        console.log("Error, possibly caused by [native code]");
        console.log(e.message);
    }
}

exports.test = test;

<p>Is there an elegant way to tell Harmony's slim arrow functions apart from regular functions <strong>and</strong> built-in functions?</p>

<p>The <a href="http://wiki.ecmascript.org/doku.php?id=harmony:arrow_function_syntax" rel="noreferrer">Harmony wiki</a> states that:</p>

<blockquote>
<p>Arrow functions are like built-in functions in that both <strong>lack .prototype</strong> and any [[Construct]] internal method. So new (() => {}) throws a TypeError but otherwise arrows are like functions</p>
</blockquote>

<p>Which means, you can test for arrow functions like:</p>

<pre><code>!(()=>{}).hasOwnProperty("prototype") // true
!(function(){}).hasOwnProperty("prototype") // false
</code></pre>

<p>But the test will also return <code>true</code> for any built-in function, e.g. <code>setTimeout</code> or <code>Math.min</code>.</p>

<p>It sort of works in Firefox if you get the source code and check if it's <code>"native code"</code>, but it doesn't seem much reliable nor portable (other browser implementations, NodeJS / iojs):</p>

<pre><code>setTimeout.toSource().indexOf("[native code]") > -1
</code></pre>

<p>The small GitHub project <a href="https://github.com/ljharb/node-is-arrow-function/blob/master/index.js" rel="noreferrer">node-is-arrow-function</a> relies on RegExp-checks against the function source code, which isn't that neat.</p>

<p><strong>edit:</strong> I gave the JavaScript parser <a href="https://github.com/marijnh/acorn" rel="noreferrer">acorn</a> a try and it seems to work quite okay - even though it's pretty overkill.</p>

<pre><code>acorn = require("./acorn");

function fn_sample(a,b){
c = (d,e) => d-e;
f = c(--a, b) * (b, a);
return f;
}

function test(fn){
fn = fn || fn_sample;
try {
acorn.parse("(" + fn.toString() + ")", {
ecmaVersion: 6,
onToken: function(token){
if(typeof token.type == "object" && token.type.type == "=>"){
console.log("ArrowFunction found", token);
}
}
});
} catch(e) {
console.log("Error, possibly caused by [native code]");
console.log(e.message);
}
}

exports.test = test;
</code></pre>
 
Top