//调用函数函数名(); //通过调用函数名来执行函数体代码
In-depth understanding of JavaScript basic functions and scope
This article brings you relevant knowledge about functions and scopes in JavaScript. A function encapsulates a block of code that can be repeatedly called and executed. The usable code scope is the scope of this name. I hope it will be useful to everyone. helpful.
1. Function
Function: It encapsulates a block of code that can be called and executed repeatedly. This code block enables a large amount of code reuse.
1.1. Use of functions Functions are divided into two steps when used:Declare the function andCall the function
①Declaration Function//声明函数function 函数名(){
//函数体代码}
Copy after login
//声明函数function 函数名(){ //函数体代码}
- function is the keyword to declare a function, must be lowercase because functions are generally defined to implement a certain function , so we usually name the function name as a verb, such as
- getSum
//调用函数函数名(); //通过调用函数名来执行函数体代码
Copy after login
//调用函数函数名(); //通过调用函数名来执行函数体代码
- When calling
- Millions Don’t forget to add parentheses Tip: If the function is not called, it will not execute itself
Note: Declaring the function itself does not execute the code, only The function body code is executed only when the function is called.
1.2. Function encapsulation- Function encapsulation is to encapsulate one or more functions through
- functions, and only provide a simple function to the outside world Interface
When declaring a function, you can Add some parameters in parentheses after the name, these parameters are called formal parameters , and when calls the function , you also need to pass the corresponding parameters, these parameters are called Arguments.
Description | |
---|---|
Form | Parameters in the formulaNumberThe parameters passed when the function is defined are not currently known |
##actual | actualparametersnumberactual parameters passed when the function calls The parameter is the one passed to the formal parameter |
function some values cannot be fixed, we can pass Parameters are passed different values when calling the function // 带参数的函数声明function 函数名(形参1, 形参2 , 形参3...) { // 可以定义任意多的参数,用逗号分隔
// 函数体}// 带参数的函数调用函数名(实参1, 实参2, 实参3...);
// 声明函数function getSum(num1,num2){
console.log(num1+num2)}// 调用函数getSum(1,3) //4getSum(6,5) //11
function call When the actual parameter value is passed to the formal parameter
-
, the formal parameter is simply understood as:
Variables that do not need to be declared -
Multiple parameters of actual parameters and formal parameters are separated by
comma (,) , 1.3.2. The number of formal parameters and actual parameters does not match
##The number of actual parameters is equal to the number of formal parameters Number | |
---|---|
The number of actual parameters is more than the number of formal parameters | Only the number of formal parameters is obtained |
If the number of actual parameters is less than the number of formal parameters | , the formal parameters are defined as undefined, and the result is NaN |
The above is the detailed content of In-depth understanding of JavaScript basic functions and scope. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
