Definition and invocation of JavaScript intrinsic functions

Source: Internet
Author: User

intrinsic function: A function defined in another function
For example:

<script>    function outer () {        function inner () {        }    }</script>

Inner () is an intrinsic function that is contained in the outer () scope, so:
Calling the inner () function inside outer () is valid,
Calling the inner () function outside of outer () is not valid.
For example:

<script>    function outer () {        console.log (' external function ');        function inner () {            console.log (' intrinsic function ');        }        Inner ();     }    Console.log (' outer (): ');    Outer ();    Console.log (' inner (): ');    Inner ();</script>

Results:
Outer ():
External functions
Intrinsic functions
Inner ():
Uncaught Referenceerror:inner is not defined (error)

-----------------------------------------------------------------------------

1. How to call 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.
Way:
① Assigning a global variable to an intrinsic function

<script>    var Globalvar;        function outer () {            console.log (' external function ');            function inner () {                console.log (' intrinsic function ');            }        Globalvar = inner;    }    Console.log ("outer ():");    Outer ();     Console.log ("Globalvar ():");     Globalvar ();     Console.log ("Inner ():");     

Results:
Outer ():
External functions
Globalvar ():
Intrinsic functions
Inner ():
Uncaught Referenceerror:inner is not defined (error)

-----------------------------------------------------------------------------
Inner (): The error is due to the fact that although the intrinsic function escaped by saving the reference in a global variable, the name of the function is still trapped in the scope of the outer ().

Calling outer () after the function definition modifies the global variable Globalvar, because Globalvar refers to INNERFN (), so when Globalvar () is executed, it is equivalent to calling inner (), resulting in the result.

② implements a reference to an intrinsic function by returning a value in the parent function (return)

<script>    function outer () {        console.log (' external function ');        function inner () {            console.log (' intrinsic function ');         }        return inner;    }    Console.log (' var res = outer (): ');    var res = outer ();     Console.log ("Res ():");    Res ();     Console.log ("outer ():");     Outer ();     Console.log ("Inner ():");    Inner ();</script>

Results:
var res = outer ():
External functions
Res ():
Intrinsic functions
Outer ():
External functions
Inner ():
Uncaught Referenceerror:inner is not defined (error)

-----------------------------------------------------------------------------
2. Variable Scope
1) Variables for intrinsic functions are limited to internal function scopes

<script>    function outer () {        console.log (' external function ')        function inner () {            var innervar = 0;            Innervar + +;            Console.log (' intrinsic function innervar= ' + Innervar);        }        return inner;    }    var res = outer ();     Res ();    Res ();    var res2 = outer ();    Res2 ();    Res2 ();</script>

Whenever this intrinsic function is called in some way, a new variable innervar is created and then incremented
Results:
External functions
intrinsic function innervar=1
intrinsic function innervar=1
External functions
intrinsic function innervar=1
intrinsic function innervar=1

-----------------------------------------------------------------------------
2) Intrinsic functions can refer to global variables like other functions

<script>    var innervar = 0;        function outer () {            console.log (' external function ')            function inner () {                Innervar + +;                Console.log (' intrinsic function innervar= ' + Innervar);            }        return inner;    }    var res = outer ();     Res ();    Res ();    var res2 = outer ();    Res2 ();    Res2 ();</script>

Results:
External functions
intrinsic function innervar=1
intrinsic function innervar=2
External functions
intrinsic function innervar=3
intrinsic function innervar=4

-----------------------------------------------------------------------------
3) The intrinsic function inherits the scope of the parent function, so the intrinsic function can also refer to the variable of the parent function

<script>    function outer () {        var innervar = 0;        Console.log (' external function ')        function inner () {            Innervar + +;            Console.log (' intrinsic function innervar= ' + Innervar);        }        return inner;    }    var res = outer ();     Res ();    Res ();    var res2 = outer ();    Res2 ();    Res2 ();</script>

Results:
External functions
intrinsic function innervar=1
intrinsic function innervar=2
External functions
intrinsic function innervar=1
intrinsic function innervar=2

Notes on learning, information on the basic jquery Tutorial (fourth edition)

Definition and invocation of JavaScript intrinsic functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.