Skip to content

function-declarations and recursive #53

Open

Description

@boris-42

// named function expression
/* Benefits:
* 1. Provides the debugger with an explicit function name: helps stack inspection.
* 2. Allows recursive functions: getData can call itself.
* Issues:
* 1. there are historic quirks with IE
*/
var getData = function getData () {
};

This is very bad idea. We have already possibility for recursion in previous both examples.

While all functions (absolutly annonimus also) can call itself using arguments.callee.

For Example:

var some = function(a){
if(a == 0 || a == 1)
return 1;

var t = arguments.callee; 
return  t(a-1) + t(a-2);  

}
alert(some(5));

Activity

  1. devinrhode2 commented on Jun 27, 2012

    @devinrhode2
    Contributor

    I believe in strict mode, arguments.callee is no longer going to be available, precisely because functions should be named. As I understood it, this was a bad idea for security reasons, and is being pushed back.

    On the same note of function declarations... I'm think we should consider a naming pattern when you are adding a function as a property within another object. Consider this code:

    var api = {};
    api.someFunction = function apiSomeFunction("?") {
      //do I recurse with api.someFunction() or apiSomeFunction()?
      //Both work, which raises the question, is there ever a scenario a which api.someFunction
        *won't be defined at the point it gets called*?
    };
    

    Lastly, I think we should establish the naming convention above, where functions as properties of other objects are camelCased from obj.subFunction to either subFunction or, objSubFunction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions