JavaScript function _ function scope __ Block chain

Source: Internet
Author: User

function in JavaScript functions are an object, and function names are references to objects. You can call it in a way

var fun = new Function ("parameter", "Return value", "function Body");

So if this is the case, it's well understood. The second function overrides the first function and can be understood to overwrite the first object with the second object. The function name is just a reference, and the Doadd variable changes the address of the reference.

var doadd = new Function ("Inum", "alert (Inum + 20)");
var doadd = new Function ("Inum", "alert (Inum + 10)");

You can use ' function name. length ' to return the number of parameters:

    function will output 1 function show
    (num) {
        alert (show.length);
    
arguments

A built-in property of a function. Arguments is similar to an array that accesses the arguments of the function, the number of arguments is not necessarily equal to the formal parameter

    Function Show (x,y,z) {
        alert (arguments.length);    Return 4
        alert (arguments[2]);        Returns the C
        alert (show.length);         Return 3
        Arguments[0] = ' f ';
        alert (x);                   return f
    };

    Show (' A ', ' B ', ' C ', ' d ');
Arguments.callee

Returns the current function reference for this arguments object
For example, the recursive algorithm, when the function itself, Arguments.callee represents the function itself

    Calculates the factorial
        function sum (num) of num {
            if (num<=1) {return
                1;
            } else{return
                Num*arguments.callee (num-1);
            }   

        Alert (SUM (4));

The function's ToString () and valueof () return function's source code call apply

Changing the scope of a function
Similarities and differences:
apply (): receives two parameters, one is the scope of the function run (this), and the other is a parameter array.
Call (): The first parameter is the same as the Apply () method, but the arguments passed to the function must be enumerated.

<script>
    var name = "Tom";

    Function Show () {
        alert ("Hello" +this.name);  This must be added otherwise it will become global variable
    }

    var obj = {
        name: "Jerry"
    }

    show.call (window);
    Show.call (obj);

</script>

Result: Hello tom-> hello Jerry this

This in JavaScript is different from C + +, this can be changed, for function fun (), if you call him with Obj.fun (), then the function fun in this is the Obj object, but called (), apply () can change this in the function. For example:

Fun.call (window);
Fun.call (this); This is the window
This becomes the window scope in the fun function

Function A body contains other function B, only in the scope of function A can call B

    <script>
        function A () {
            B ();
            Function B () {
                alert ("This is function B");
            }
        A ();
    </script>
no block-level scope

if () {} does not have the function of enclosing scope

    if (true) {
        var temp = ' sss ';
    }
    Alert (temp);
The result is SSS.

for (;;) {} I and internal variables are global

    for (Var i=0;i<10;i++) {
        var temps = ' for ';
    }
    Alert ("I:" +i+ "temps:" +temps);
The result is i:10 temps:for.

In a function, the variable plus var is a local variable, and no var is a global variable. When a function uses a variable, it searches for the local scope first, and searches for the element in the previous scope if no search is found. Therefore, accessing local variables is faster than global variables

Ps:

1, the base type cannot add attributes (only object is a reference)
2, the base type is saved in stack memory, the reference type is saved in heap memory, the basic type of assignment copies the entire content, and the reference type assignment is just the copy of the address
3, all the functions of the parameters are passed by value, the object passed the value is the address so that the external object will also change
4,instanceof type judgment type cannot check base type

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.