Home Web Front-end JS Tutorial Summary of basic JavaScript knowledge (6) Functions and initial scope (Part 1)

Summary of basic JavaScript knowledge (6) Functions and initial scope (Part 1)

Mar 10, 2018 pm 12:07 PM
javascript js function

This time I will bring you a basic JavaScript knowledge summary. There are a total of eleven knowledge points. Basic JavaScript knowledge summary (6) Function, initial scope (Part 1). Here are practical cases. Let’s take a look.

Function

Return value

//函数声明//第一种function box(参数){    //内容};//第二种叫命名函数表达式var box = function test(参数){    //内容}
box.name-->test//第二种写法function后面就成了表达式,有没有名字无所谓,所以延伸出了第三种写法//第三种叫匿名函数表达式-->函数表达式var box = function (参数){    //内容}
box.name -->box//函数执行box();
Copy after login

Parameter

//形式参数-->形参function test(a,b){    var c= a+b    console.log(c);//等于3}//实际参数--实参test(1,2);//不定参数 例子一function test(a){    //a 就等于1
    //2,3不用管
    隐式的方法arguments[1,2,3]实参列表
    找到多余的实际参数
}
test(1,2,3);//不定参数 例子二function test(a,b,c,d){    //a 等于1
    //b 等于2
    //c 等于3
    //d 等于undefined
    找到形参的长度sum.length
}
test(1,2,3);
//不定参的好处var resultfunction sum(){    for(var i = 0; i<arguments.length){
        resuit += arguments[i];
        i++;
    }    
    console.log(result);
    
}
sum(1,2,3,4,5,6,7,8,9)
function sum(a,b){
    a = 2;    console.log(arguments[0]);//打印出2,参数a改变了}
sum(1,2);function sum(a,b){
    a = 2;    arguments[0] = 3
    console.log(arguments[0]);//打印出3,参数a改变了,可以相互改变}
sum(1,2);//JS的映射规则
function sum(a,b){
    b = 2;    arguments[1];    console.log(arguments[0]);
}
sum(1);//JS的映射规则
Copy after login

prints out undefined. When the actual parameter list is passed in, there are several of them. Even if I let If b is equal to 2, it will not be added to arguments because it does not exist at all. At this time, b is used as a variable. It does not map to the actual parameters because the formal parameter has one more b than the actual parameter. Only When they are equal, they will have mapping rules. When they are not equal, if there are too many formal parameters, they will not correspond to actual parameters, and there will be no mapping between them.

End condition plus return value return

End the function, if not written, the system comes with return by default

Return the value to the outside of the function

function sum(){    return 123}var box = sum();-->返回123
Copy after login

Believe it After reading the case in this article, you have mastered the method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

Basic JavaScript knowledge summary (4) Conditional statements, loop statements

Basic JavaScript knowledge Summary (2) Introduction, variables, value types, operators

Summary of basic JavaScript knowledge (3) Comparison operators, logical operators

The above is the detailed content of Summary of basic JavaScript knowledge (6) Functions and initial scope (Part 1). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Tips for dynamically creating new functions in golang functions

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

Considerations for parameter order in C++ function naming

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

How to write efficient and maintainable functions in Java?

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

Complete collection of excel function formulas

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

What are the benefits of C++ functions returning reference types?

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

What is the difference between custom PHP functions and predefined functions?

Advanced usage of reference parameters and pointer parameters in C++ functions Advanced usage of reference parameters and pointer parameters in C++ functions Apr 21, 2024 am 09:39 AM

Advanced usage of reference parameters and pointer parameters in C++ functions

See all articles