Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

self-defining function(also called lazy function definition) when using function declaration

In the "JavaScript Patterns" book by Stoyan Stefanov, there's a part about Self-Defining Function.

var scareMe = function(){
    console.log("Boo!");
    scareMe = function(){
        console.log("Double Boo!"); 
    }
}

scareMe();//==>Boo!
scareMe();//==>Double Boo!

It works as I expected. But I modify the scareMe function as following:

function scareMe(){
     console.log("Boo!");
     function scareMe(){
         console.log("Double Boo!");
     }
 }

 scareMe();//==>Boo!
 scareMe();//==>Boo!

Problem:

  1. what's the difference between them?
  2. In the second case, why the output is not "Double Boo!", but "Boo!"
like image 846
jason Avatar asked Sep 30 '13 01:09

jason


People also ask

What is function declaration and definition?

A function is a subprogram that returns a value. The data type of the value is the data type of the function. A function invocation (or call) is an expression, whose data type is that of the function. Before invoking a function, you must declare and define it.

How do you declare a function in syntax?

A function consist of two parts: Declaration: the function's name, return type, and parameters (if any) Definition: the body of the function (code to be executed)

What is the purpose of defining functions?

Quite often when carrying out a project, some set of tasks will need to be repeated with different input. To ensure that the analyses are carried out the same way each time, it is important to wrap the code into a named function that is called each time it's needed.

How does JavaScript function work?

A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.


1 Answers

The first scareMe function when invoked overwrite its own behavior by creating another function scareMe inside it, which overwrites the one in the upper scope, so the definition of original scareMe changes, i have see this approach been used if you want to do a first time set up in an application and want to change its behavior all over right after setting it up.

If you had defined:

var scareMe = function(){
    console.log("Boo!");
    var scareMe = function(){ //define it with var
        console.log("Double boo!"); 
    }
}

scareMe();//==>Boo!
scareMe();//==>Boo! //you will see the behavior as that of the second one.

Also one practical implementation of a one time setup:

var scareMe = function(){
    console.log("Boo!");

   //I have done my job now. I am no longer needed.
    scareMe = undefined;

}

scareMe();//==>Boo!
scareMe();//==> oops error

Second Case you are creating a new function with the name scareMe whose scope is only within the function, it doesn't overwrite itself.

Try this one for instance:

function scareMe(){
     console.log("Boo!");
     function scareMe(){
         console.log("Double bool!");
     }
    scareMe(); //Now this invokes the once defined inside the scope of this function itself.
 }

 scareMe();//==>Boo! and Double bool!
like image 82
PSL Avatar answered Nov 19 '22 16:11

PSL