当前位置:网站首页>JS functions
JS functions
2022-06-25 14:12:00 【Rabbit^-^】
function
Page statements are executed sequentially , Defining functions facilitates code encapsulation , You can deal with js Encapsulated in a function , Reduce code writing , Support multi page calling , You can also execute through events
Definition of function
Ordinary function
function Function name (){
Execute statement
}
call : Function name ()
Functions with parameters
function Function name (a,b,c){
Execute statement
}
call : Function name (1,2,3)
Function with return value
function Function name (a,b,c){
Execute statement
return a+b
}
call :var v= Function name (1,2,3)
recursive : Call yourself , Simulation cycle
function Function name (num){
if(num==1){
return 1
}
return Function name (num-1)
}
call :var v= Function name (10)
for example :function fun( num){
if(num<=1){
return 1
}
return num*fun(num-1)
}
var v=fun(6)
console.log(v)
Js No overloading
Can be in the same scope , Define a function with the same name , But do different things ,js Can not be
for example :function fun(a,b){
console.log(" first ")
}
function fun(a,b,c){
console.log(" the second ")
}
fun(1,2)
The second overwrites the first
Function declaration
1、 Function declaration
for example :function fun(a,b){// Function declaration
console.log(" first ")
}
fun()
2、 Function expression
for example :var fun1=function(a,b){// Function expression
console.log(" first ")
}
fun1()
3、 Anonymous functions Put in front and execute first
for example :
(function(){
console.log(" This is an anonymous function ")
})()
Variable array ( Variable parameters )
be called arguments, It's like an array , Used to receive multiple parameter values under the function , Can only be used in functions , But you can't change it like an array
for example : function fun(){
console.log(arguments)
console.log(arguments.length)//6
console.log(arguments[2],arguments[4])//3 5
}
fun(1,2,3,4,5,6)
Be careful :arguments Although similar to an array , But it's a fixed length , Unlike arrays, which are variable length , For the array operation function , Not available except for traversal , You can only change variables and values of variable arrays
边栏推荐
猜你喜欢

戴尔电脑cpu温度过高怎么办

一次性讲清楚 Handler 可能导致的内存泄漏和解决办法 | 开发者说·DTalk

历史上的今天:网易成立;首届消费电子展召开;世界上第一次网络直播

Network remote access using raspberry pie

shell 运算符

‘nvidia-smi‘ 不是内部或外部命令,也不是可运行的程序或批处理文件

Classifier and cross entropy loss function

权益NFT开创者Hash Eagle如何重新定义NFT,用权益赋能长续价值?

DE2-115 FPGA开发板的VGA显示

Settings the PC must be turned on
随机推荐
Where can the brightness of win7 display screen be adjusted
Openstack learning notes (I)
As a software testing engineer, how do you think to ensure software quality?
分类器与cross entropy loss函数
turtlebot+lms111+gmapping实践
Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
中国电池技术取得重大突破,日韩美都落后了,中国巩固了领先优势
Is qiniu regular? Is it safe to open a stock account?
'NVIDIA SMI' is not an internal or external command, nor is it a runnable program or batch file
Table de hachage, conflit de hachage
程序員為什麼要軟一點?
Use of bind() in JS and handwritten bind()
Is it safe to open an account with tongdaxin stock?
Two methods to rollback the code in pycharm to the specified version (with screenshot)
Untiy force refresh UI
使用调试工具调试博图TCP连接所遇到的问题
untiy强制刷新UI
测一测你的挣钱能力有多强?未来的你将从事什么职业?
Sigmoid function sigmoid derivation
[open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release