Home > Web Front-end > JS Tutorial > body text

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

php中世界最好的语言
Release: 2018-03-10 12:07:01
Original
1039 people have browsed it

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
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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!