当前位置:网站首页>Functions in JS
Functions in JS
2022-07-25 11:47:00 【c_ perfectworld】
function
Realize the reuse of code
Two ways to declare
- Use function keywords to customize functions ( Name the function )
- Function expression ( Anonymous functions )
var Variable name = function () {}Use steps
- Declare functions
- Call function
function Function name () { The body of the function }Be careful :
- function Key to declare function , All lowercase
- A function is to do something , Function names usually use verbs
- The function does not call and does not execute itself
Call function
Function name ();
Encapsulation of functions
- Function encapsulation is to pass one or more functions Function to encapsulate , It only provides a simple function interface to the outside world
- Similar to express packaging
The parameters of the function
Formal parameters and actual parameters
function Function name ( Shape parameter 1, Shape parameter 2...) {
// The body of the function
}
Function name ( Actual parameters 1, Actual parameters 2);When you declare a function , You can add some parameters after the parentheses , These parameters are called Shape parameter , When the function is called , You also need to pass the corresponding parameters , These parameters are called Actual parameters

Parameter function : Internal function Some values are not fixed , You can use parameters in Pass... When calling a function Different values go in
Be careful :
- Multiple parameters are separated by commas
- Formal parameters can be regarded as undeclared variables
The number of function parameters and arguments does not match
If the number of arguments is more than the number of formal parameters , Will get the number of formal parameters
If the number of arguments is less than the number of formal parameters , Redundant formal parameters are defined as undefined, The end result is NaN
Summary
- Functions can take parameters or no parameters
- When you declare a function , In parentheses of function name are formal parameters , The default value is undefined
- When you call a function , Inside the parentheses of the function name is the argument
- Multiple parameters are separated by commas
- The number of formal parameters can not match the number of actual parameters
The return value of the function
return sentence
- A function is just a function , The final result needs to be returned to the caller function name of the function () adopt return
- As long as the function encounters return, Just return the result to the caller of the function Function name () = return The latter result
In actual development , Often use a variable to receive the return value of a function
return Terminate function
return The subsequent code is not executed
return The return value of
return Only one value can be returned , If you separate multiple values with commas , Subject to the last one
If the function has return, return return Value after , If the function doesn't have return Then return to undefined
break return continue The difference between
- break: End the current cycle
- continue: Jump out of this cycle , Continue with next cycle
- return: Not only exit the loop body , And return to return Value in statement , At the same time, you can also end the code in the current function body
arguments Use
When it is uncertain how many parameters are passed , It can be used arguments obtain , stay js in ,arguments It is a of the current function Built-in objects . All functions have a built-in arguments object ,arguments object Stores all arguments passed
arguments The display form is a pseudo array , It can be traversed , It has the following characteristics :
- have length attribute
- Store data by index
- Does not have an array push、pop Other methods
Only functions have arguments object
Function expression declares function
- fun Is a variable name. , It's not a function name
- Function expressions are declared in much the same way as variables , But the value stored in the variable , What is stored in the function expression is the function
- Function expressions can also pass arguments
边栏推荐
- Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)
- 小微企业智能名片管理小程序
- leetcode 剑指 Offer 27. 二叉树的镜像
- Some errors of tensorflow calling multiple GPUs
- Information management system for typical works of urban sculpture (picture sharing system SSM)
- Wiznet embedded Ethernet technology training open class (free!!!)
- W5500 multi node connection
- Reinforcement learning (IV)
- Dynamic planning question 05_ Missile interception
- Maskgae: masked graph modeling meets graph autoencoders
猜你喜欢
随机推荐
JVM性能调优方法
Maskgae: masked graph modeling meets graph autoencoders
基于cornerstone.js的dicom医学影像查看浏览功能
JS中的数组
SQL language (III)
Emmet syntax quick query syntax basic syntax part
Onenet platform control w5500 development board LED light
Small program of vegetable distribution in community
Detailed explanation of zero basis from macro to micro Bert
【mysql学习09】
Brief description of model deployment
菜单栏+状态栏+工具栏==PYQT5
Similarity matrix, diagonalization condition
动态规划问题05_导弹拦截
擎创科技加入龙蜥社区,共建智能运维平台新生态
使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
varest蓝图设置json
WIZnet W5500系列培训活动之“MQTT协议讲解和实践(接入OneNET)”
Information management system for typical works of urban sculpture (picture sharing system SSM)
![[MySQL learning 09]](/img/27/2578f320789ed32552d6f69f14a151.png)








