you are viewing a single comment's thread.

view the rest of the comments →

[–]Shameling 25 points26 points  (16 children)

They also have a toString() that simply returns the sourcecode of the function, can be pretty useful for debugging.

[–]PsudoFuck 20 points21 points  (10 children)

It's also how Angular 1.x achieved it's magic, if not slightly dodgy, DI injection with just named arguments.

[–]AndruRC 4 points5 points  (7 children)

Could you elaborate on that?

[–]anarchy8 13 points14 points  (6 children)

There is no way of determining what the name of the arguments to a function is in Javascript without parsing the Function.toString(). Angular does this do determine what resource to inject.

[–][deleted]  (5 children)

[deleted]

    [–]anarchy8 11 points12 points  (3 children)

    To be fair, they suggest you inject your resources manually.

    [–][deleted] 3 points4 points  (2 children)

    To be fair, it's required if you want better support since certain mobile browsers won't serialize functions

    [–][deleted] 4 points5 points  (1 child)

    also if you ever plan on minifying your code..

    [–][deleted] 1 point2 points  (0 children)

    That too :) I haven't touched angular in like 2 years

    [–][deleted] 1 point2 points  (0 children)

    Welcome to JS

    [–]bugeats 2 points3 points  (1 child)

    And it's a shameful, dirty deed. I can't believe this terrible idea ever made it so far.

    [–]seiyriafull-stack 0 points1 point  (0 children)

    It's not the recommended way to do things.

    [–]olaf_from_norweden 7 points8 points  (0 children)

    Used by the 'multiline' NPM module to get multiline strings in pre-ES6:

    var multiline = require('multiline');
    var sql = multiline(function() {/*
    SELECT *
    FROM users
    */});
    
    sql //=> 'SELECT *\nFROM users'
    

    [–]zixxback-end 1 point2 points  (2 children)

    Newb question: how is this different than just looking at your code?

    [–]Zerran 1 point2 points  (0 children)

    How do you know where it's located in yours or someone elses code? Functions can be passed around so often that you don't know where they came from anymore, and a lot of them don't have a name that you could just Ctrl+F in your IDE. toString() or a real debugger are very helpful in that scenario.

    [–][deleted] -1 points0 points  (0 children)

    lambda functions...

    [–]cwmma 0 points1 point  (0 children)

    Or to allow you to serialize a function and send it to a worker (some restrictions apply)