In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the usage of JavaScript functions is, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
1. Declaring function//declaring function function name () { //function body code}
function is a keyword that declares a function. It must be lowercase. Since functions are generally defined to implement a certain function, we usually name function names as verbs, such as getSum.
2. Call function//call function name (); //execute function body code by calling function name
Declare code that the function itself does not execute, and only executes the function body code when the function is called.
3. function parameters
When declaring a function, you can add some parameters in parentheses after the function name, which are called parameters, and when calling the function, you also need to pass corresponding parameters, which are called arguments.
Parameter Description Parameters in the form Parameters passed when the function is defined Parameters currently unknown What arguments are actually parameters parameters passed when the function is called Parameters passed to parameters
The role of parameters: some values inside the function cannot be fixed, we can pass different values through the parameters when calling the function.
The mismatch between the number of function parameters and actual parameters
The number of arguments is equal to the number of parameters. The output is correct. The number of arguments is more than the number of parameters. The number of arguments is less than the number of parameters. The parameter definition is undefined. The result is NaNfunction sum(num1,num2) { console.log(num1+num2);}sum(100,200); //The number of arguments equals the number of parameters and outputs the correct result sum(100,400,500,700); //The number of arguments exceeds the number of parameters and only the number of parameters sum(200) is taken; //The number of arguments less than the number of parameters is defined as undefined, and the result is NaN
In JavaScript, the default value for a parameter is undefined.
4. the return value of the function
Return is often used to return function values
return terminating function
function add(num1,num2) { //function body return num1+num2; //Note: The code after return is not executed alert ('there is a return before, I will not be executed');}var resNum = add(21,6); //Call the function, pass two arguments, and receive the function return value alert(resNum) through resNum; //27
If the function has a return, it returns the value after return; if there is no return, it returns undefined.
Break, Continue, Return Difference
break: ends the current loop body (e.g. for, while)
continue: jump out of this loop and continue to execute the next loop (e.g. for, while)
return: Not only can you exit the loop, but you can also return the value in the return statement and end the code in the current function body.
5. Use of arguments
When we are unsure how many arguments are passed, we can get them with arguments, which in JavaScript is actually a built-in object of the current function. All functions have a built-in arguments object that stores all arguments passed.
Document function fn() { console.log(arguments); //all arguments passed in are stored console.log(arguments.length); console.log(arguments[2]); //arguments can be iterated over as arrays for (var i = 0; i
< arguments.length; i++) { console.log(arguments[i]); } } fn(1, 2, 3); fn(1, 2, 3, 4, 5); //伪数组 并不是真正意义上的数组 //1. 具有数组的length 属性 //2. 按照索引的方式进行存储的 //3. 它没有真正数组的一些方法 如pop() push()等等6. 函数可以调用另外一个函数function fn1() { console.log(111); fn2(); console.log('fn1');}function fn2() { console.log(222); console.log('fn2');}fn1();7. 函数的两种声明方式//1. 利用函数关键字自定义函数(命名函数)function fn() {}fn();//2. 函数表达式(匿名函数)//var 变量名 = function() {};var fun = function(aru) { console.log('我是函数表达式'); console.log(aru);}fun('FG');//(1) fun是变量名 不是函数名//(2) 函数表达式声明方式跟声明变量差不多,只不过变量里面存的是值 而函数表达式里面存的是函数//(3) 函数表达式也可以进行传递参数总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!
1. 声明函数//声明函数function 函数名() { //函数体代码}
function是声明函数的关键字,必须小写由于函数一般是为了实现某个功能才定义的,所以通常我们将函数名命名为动词,比如getSum
2. 调用函数//调用函数函数名(); //通过调用函数名来执行函数体代码
声明函数本身并不会执行的代码,只有调用函数时才会执行函数体代码。
3. 函数的参数
在声明函数时,可以在函数名称后面的小括号中添加一些参数,这些参数被称为形参,而在调用该函数时,同样也需要传递相应的参数,这些参数被称为实参。
参数说明形参形式上的参数 函数定义的时候 传递的参数 当前并不知道是什么实参实际上的参数 函数调用的时候传递的参数 实参是传递给形参的
参数的作用:在函数的内部某些值不能固定,我们可以通过参数在调用函数时传递不同的值进去。
函数形参和实参个数不匹配问题
参数个数说明实参个数等于形参个数输出正确结果实参个数多于形参个数只取到形参的个数实参个数少于形参个数多的形参定义为undefined,结果为NaNfunction sum(num1,num2) { console.log(num1+num2);}sum(100,200); //实参个数等于形参个数 输出正确结果 sum(100,400,500,700); //实参个数多于形参个数 只取到形参的个数sum(200); //实参个数少于形参个数 多的形参定义为undefined,结果为NaN
在JavaScript中,形参的默认值是undefined 。
4. 函数的返回值
经常用return返回函数值
return终止函数
function add(num1,num2) { //函数体 return num1+num2; //注意:return后的代码不执行 alert('前面有return,我不会被执行');}var resNum = add(21,6); //调用函数,传入两个实参,并通过resNum接收函数返回值alert(resNum); //27
如果函数有return,则返回return后面的值;如果没有return,则返回 undefined 。
break,continue,return的区别
break:结束当前的循环体(如for、while)
continue:跳出本次循环,继续执行下次循环(如for、while)
return:不仅可以退出循环,还能够返回return语句中的值,同时还可以结束当前的函数体内的代码。
5. arguments的使用
当我们不确定有多少个参数传递的时候,可以用arguments来获取,在JavaScript中,arguments实际上它是当前函数的一个内置对象。所有函数都内置了一个arguments对象,arguments对象中存储了传递的所有实参。
Document function fn() { console.log(arguments); //里面存储了所有传递过来的实参 console.log(arguments.length); console.log(arguments[2]); //可以按照数组的方式遍历arguments for (var i = 0; i
< arguments.length; i++) { console.log(arguments[i]); } } fn(1, 2, 3); fn(1, 2, 3, 4, 5); //伪数组 并不是真正意义上的数组 //1. 具有数组的length 属性 //2. 按照索引的方式进行存储的 //3. 它没有真正数组的一些方法 如pop() push()等等6. function fn1() { console.log(111); fn2(); console.log('fn1');}function fn2() { console.log(222); console.log('fn2');}fn1();7. The requested URL//was not found on this server. Function fn() {}fn();//2. function expression (anonymous function)//var variable name = function() {};var fun = function(aru) { console.log ('I am a function expression'); console.log(aru);}fun('FG');//(1) fun is the variable name is not the function name//(2) function expression declaration method is similar to the declaration variable, but the variable inside is the value and the function expression inside is the function//(3) function expression can also pass parameters about "JavaScript function usage what" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, Please share it with more people.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.