JavaScript — function

Time:2021-5-4

1、 Function definition and call (same as Python)

//Nonparametric function
function f1() {
  console.log("Hello world!");
};
f1();

//Parametric function
function f2(a, b) {
  console.log(arguments);  //  Built in arguments object
  console.log(arguments.length);

  console.log(arguments[0],arguments[1]);
  console.log(a, b);
};
f2(10,20);

//Function with return value
function sum(a, b){
  return a + b;
}
sum(1, 2);  //  Call function

//Anonymous function
var sum = function(a, b){
  return a + b;
}
sum(1, 2);

//Execute function immediately
(function(a, b){
  return a + b;
})(1, 2);

 

2、 Arguments argument in function

function add(a,b){
  console.log(a+b);
  console.log(arguments.length)
}

add(1,2)   //  3  2 

Note: the function can only return one value. If you want to return multiple values, you can only return them in an array or object

3、 Global and local variables of functions

#Local variables:

The variable declared inside the JavaScript function (using VAR) is a local variable, so it can only be accessed inside the function (the scope of the variable is inside the function). As long as the function runs, local variables will be deleted.

#Global variables:
Variables declared outside the function are global variables that can be accessed by all scripts and functions on the web page.

#Variable life cycle:
The lifetime of JavaScript variables begins at the time they are declared.
Local variables are deleted after the function runs.
Global variables are deleted when the page is closed.

4、 Scope

First of all, find the variable inside the function, if not, find the outer function, and gradually find the outermost layer. In addition, the scope of the function is fixed at the definition stage, independent of the call location

//For example, the usage is consistent with the order in which Python looks up variables
var city = "BeiJing";
function f() {
  var city = "ShangHai";
  function inner(){
    var city = "ShenZhen";
    console.log(city);
  }
  inner();
}

f();  //ShenZhen


var city = "BeiJing";
function Bar() {
  console.log(city);
}
function f() {
  var city = "ShangHai";
  return Bar;
}
var ret = f();
ret();  // BeiJing


var city = "BeiJing";
function f(){
    var city = "ShangHai";
    function inner(){
        console.log(city);
    }
    return inner;
}
var ret = f();
ret();  //ShangHai

5、 Variable promotion and function promotion in JavaScript

1. There are only two scopes in JS

Global scope, function scope

  ps:Before ES6, JS had no block level scope.

2. What is no block level scope?

 3. What is variable promotion?

In JS, code execution is divided into two steps: 1. Parsing; 2. Step by step execution

Then variable promotion means that the variable declaration will be promoted to the top of the scope, that is, no matter where the variable is declared in the scope, it will be promoted to the top of the scope.

 

For example:

 

4. What is function promotion?

Example 1:

fn();  // It can be called normally here
console.log(fn);
function fn(){
    Console.log ("ha ha ha ha ha")
}
fn(); // It can also be called normally here

//Output results:

Ha ha ha ha
{
    Console.log ("ha ha ha ha ha")
}
Ha ha ha ha


//Note: the function declaration will promote the declaration and definition of the function to the top of the scope.

Example 2:

Functions declared by function expressions

console.log(fn); //undefined
var fn=function (){
    Console.log ("example 2")
};
console.log(fn);

//Output results:
ƒ (){
    Console.log ("example 2")
}

Example 3:

If the function declaration and the variable declaration use the same variable name, the function declaration takes precedence over the variable declaration

console.log(fn);
Function fn () {"example 3"};
var fn="hello";
console.log(fn);

//Output results:
ƒ  FN () {example 3}
hello

Conclusion:

#1: All declarations are promoted to the top of the scope.

#2: The same variable will only be declared once, and others will be ignored.

#3: The priority of function declaration is higher than that of variable declaration, and function declaration and function definition are promoted together.

 

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]