JavaScript Basics (4) Functions

function

function definition

//第一种:(函数的声明)第一种定义方法最强大,定义完毕后,在哪里使用都可以,无位置限制。
function fn1(){
    console.log("我是第一种定义方法!");
}

//第二种(函数表达式:匿名函数) :后两种定义方法是有局限性的。(使用函数必须在定义函数之后)
var fn2 = function (){
    console.log("我是第二种定义方法!");
};  // 注意分号

function (){
   console.log("我是第二种定义方法!");
}();  // 第二种方式的调用方式之一:函数的自调用

//第三种
var fn3 = new Function("console.log('我是第三种定义方法!')");

function call

函数名();

Function name

  • Follow CamelCase.
  • It cannot have the same name (function overloading), otherwise the latter function will overwrite the former function.
//打印函数名,就等于打印整个函数。
console.log(fn);
//打印执行函数,就等于打印函数的返回值。
console.log(fn()); 

Formal and actual parameters

  • Formal parameters do not need to write var.
  • The number of formal parameters and the number of actual parameters can be inconsistent.

return value

1. If the function does not explicitly use the return statement, then the function has a default return value: undefined
2. If the function uses the return statement, but there is no value after the return, then the return value of the function is also: undefined.

Variables and scope

Global variables : variables defined with var in script and variables without var (all scripts share their globality, there is no block-level scope concept in js, only global scope and local scope).

Implicit global variables : variables without var in script.

function fn(){
  var a = b = c = 1;       // b和c就是隐式全局变量(等号)
  var a = 1; b = 2; c = 3;   // b和c就是隐式全局变量(分号)
  var a = 1 , b = 2 , c = 3;  // b和c就不是隐式全局变量(逗号)
}

(Global variables cannot be deleted, implicit global variables can be deleted)

var num1 = 10;
num = 20;
delete num1;
delete num2;
console.log(typeof num1); // number
console.log(typeof num2); // undefined

Local variables : variables and formal parameters defined with var inside a function .

Variable declaration hoisting (preparse)

Role: Check for syntax errors. When the js parser loads the page, it first checks for syntax errors on the page. Hoist variable declarations. Variable declaration hoisting and function overall hoisting.

Variable promotion

Only the variable name is promoted, not the variable value.

consolas.log(aaa);// 打印的结果是 undefined ,因为只提升变量名,不提升变量值。
var aaa = 111; 

Within the scope of the function, the same applies.

function boost

Function directly defined method: overall promotion (the first method of function definition mentioned above).

fn();

var aaa = 111;
function fn(){
    //变量声明提升在函数内部照样实用。
    //函数的就近原则(局部变量作用域),打印的aaa不是111,而是 undefined。
    console.log(aaa); // undefined
    var aaa = 222;
}

Preparse will be chunked:

If the functions in multiple pairs of script tags have the same name, the pre-parsing will not conflict. That is, the pre-parsed scope is each script tag.

var is promoted first, then function is promoted:

Example:

console.log(a); // 输出a函数体
function a() {
    console.log("aaaaa");
}
var a = 1;
console.log(a); // 输出1

When printing the first result, var will be promoted, and then function will be promoted again, but the function a and variable a have the same name, and the a of function overwrites the variable a later, so the first output is the function body of a.

After the second var a = 1; is promoted, this position is equivalent to only a = 1; assignment, so the second prints 1.

anonymous function

//1.直接调用
(function (){
    console.log(1);
})();

//2.绑定事件
document.onclick = function () {
    alert(1);
}

//3.定时器
setInterval(function () {
    console.log(444);
},1000);

Guess you like

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