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();
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的映射规则
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
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!