js basics (functions)

 

A function is an object. Functions can encapsulate functions. When using typeof to check a function object, it will return function.

 

Create a function object

var function = new Function();

The code to be encapsulated can be passed to the constructor as a string

var function = new Function("console.log('xxxxxxxx');");;

Syntax for calling a function: function object(), function()

 

Use a function declaration to create a function

grammar:

  function function name (parameter list) {

    function body;

  }

 

Use function expressions to create a function:

grammar:

  var fun = function (form attendance table) {

    function body;

  }

 

When calling a function, the parser will not check the type of the actual parameters, nor the number of actual parameters, the redundant actual parameters will not be assigned, the number of actual parameters is less than the number of formal parameters, the formal parameters without assignment will be undefined

 

If you don't write return after it, it is equivalent to returning undefined. If you don't write a return statement, it will also return an undefined

The return value of return can be of any type, it can be an object or a function

function fun3(){
     // declare another function inside the function 
    function fun4(){
        alert( " I am fun4 " );
    }         
        // Return the fun4 function object as the return value 
    return fun4;
}

a = fun3();
//a();
fun3()();    

Execute the function immediately:

function(){
    alert("xxxxxx");
}
//The above is wrong spelling
(function(){
    alert("xxxxxx");
})();
// execute immediately

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326371202&siteId=291194637