An intrinsic function, a function defined in another function
function outer () { console.log ("Out"); function inner () { console.log ("in"); } } Outer (); // inner (); // Error: Inner is not defined
The inner here is an intrinsic function defined in outer, whose scope is inside the function outer, and the outer run is invalid.
1. How do I invoke an intrinsic function anywhere?
JavaScript allows functions to be passed like any type of data, meaning that internal functions in JavaScript can escape the definition of their external functions.
① Assigning a global variable to an intrinsic function
var bgg; function outer () { console.log ("Out"); function inner () { console.log ("in"); } BGG=inner; } Outer (); // bgg (); // inch
② implements a reference to an intrinsic function by returning a value in the parent function (return)
function outer () { Console.log ( ' out ' function inner () { Console.log ( ' in ' return inner; var res = outer (); // out res (); // in outer (); // out
In the example here, execute out (), just run the function, what is the result? PrintOut out, the function inner as the return value of the function out. var res = out () What is this sentence doing again?
This way, the equals sign evaluates the expression on its right and assigns the value of the right expression to the left of the equal sign. The return value of the Out function run is the function inner, so the next step, run res, prints in.
function outer () { console.log ("Out"); return function inner () { console.log ("in") } } outer () (); // out
In this example, the function out is executed, and the function return value inner function is returned to Outer,out () (), which is equivalent to a self-executing function.
Note: 1.return is executed at the end of the function statement and returns the value of the expression as the result of the function
2. Return null value, syntax: return;
In general, return Return:false for the event handler function; , the function is to block the default event behavior and cancel the default action, for example, by clicking on a <a> element by default, the page jumps to the page specified by the element href attribute, and when you use return false, it is equivalent to a terminator and return true; Equivalent to an executor.
A little misunderstanding of his own at the time: when returning an expression, the value of the expression is used as the result of the function, which we cannot see, and only when we print out the output can we see the return value of the function. The function's run result and return value are not the same "thing".
Definition and invocation of JavaScript intrinsic functions