locked
What is a function in JavaScript RRS feed

  • General discussion

  • Hi there,

    I am a C++ programmer, and I am learning JavaScript now.

    I don't quite understand what is a function in JS.

    As far as I know, everything is an object in JS (including function).

    An object is a hash-like structure in JS so we can add properties to an object at run-time.

    It makes sense to me.


    However, a function is also an object which means that a function is also a hash-like structure.

    How can a hash-like structure simulate a function call?

    I just don't get it!


    In my opinion, parameters and statements of a function cloud be elements of a hash-like structure.

    We can use eval() to execute those statements in the hash-like structure.

    But I am not sure.


    What is a function in JS under the hood?

    thanks in advance.

    Monday, April 29, 2013 2:44 PM

All replies

  • I suppose fundamentally, you can answer your question with a question. That question is, if you don't name the function how would you go about calling the function explicitly elsewhere?

    Of course, the example you provided would still work without a name so I'd suggest that it's as much about personal preference on the part of the developer as it is about anything functional. I personally like to name my functions for easier debugging, reuse and readability as well as for the usefulness of explicitly calling them.

    There are also the obvious performance benefits to be gained by creating functions once and reusing them.

    The main benefit for naming the function is in allowing it to be self-referential, especially for recursion:

    If you want to refer to the current function inside the function body, you need to create a named function expression. This name is then local only to the function body (scope). This also avoids using the non-standard arguments.callee property.

    var math = {
      'factorial': function factorial(n) {
        if (n <= 1)
          return 1;
        return n * factorial(n - 1);
      }
    };

    That is, without it having to be aware of its holding object and referencing property name, which can change:

    // a contrived and hopefully unrealistic example
    
    function foo() {
        this.bar = function () {
            return this.bar;
        };
    }
    
    console.log(new foo().bar());        // Function
    console.log(new foo().bar.call({})); // undefined, `{}` doesn't have a `bar`
    As previously noted, it's an available solution to the partial "outlawing" of

    Monday, April 29, 2013 4:36 PM
  • Hi there,

    An object in JS is implemented by a hash-like data structure, so

    var person=new Object();
    person.firstname="John";
    person.age=50;
    person.eyecolor="blue";

    is equal to

    var person=new Object();
    person["firstname"]="John";
    person["age"]=50;
    person["eyecolor"]="blue";

    And a function is also implemented by a hash-like data structure, so I guess

    function foo(var name)
    {
        alert("hello, " + name);
    }

    might be equal to (not sure)

    foo["parameter"] = name;
    foo["statement"] = 'alert("hello, " + name);'

    When we try to invoke the function foo, it might be (not sure)

    eval(Foo["statement"]);

    Is it correct?

    Tuesday, April 30, 2013 8:09 AM