当前位置:网站首页>JS notes (III)
JS notes (III)
2022-07-03 21:22:00 【Deduct 1 to send hell fire】
Catalog
2.2 The parameters of the function
2.3 The return value of the function
2.5 Two ways to declare functions
1. Array
1.1 Create array
JavaScript There are two ways to create arrays in :
- utilize new Create array
- Create an array using array literals
utilize new Create array
var Array name = new Array(); // Create a new empty array
Create an array using array literals
// 1. Create an empty array using array literal
var Array name =[];
// 2. Create an array with an initial value using array literal
var Array name =['a','b','b'];
// 3. Array can store any type of data , Like strings , Numbers , Boolean value, etc
var arrStus =['haha',6,true,6.1,'a'];
- The literal of the array is square brackets
[]
- Declaring an array and assigning values is called array initialization
1.2 Index of array
Indexes : The ordinal number used to access an array element ( Array index from 0 Start )
// Define an array
var arr = [1,2,3];
// Gets the number... In the array 3 Elements
alert(arr[2]);
1.3 Traversal array
adopt for The circular index traverses each item in the array
// for Loop through groups
var arr = ['a','b', 'c'];
for (var i = 0; i < arr.length; i++){
console.log(arr[i]);
}
1.4 Length of array
Use “ Array name .length” You can return the number of array elements
var arr = [1,2,3];
alert(arr.length); // Output 3
length Attributes will dynamically change with the number of elements in the array
1.5 Add array elements
1 It can be modified by length Length to achieve the purpose of array expansion
var arr = ['a','b','c'];
arr.length =5;
console.log(arr);
The index number is 3 4 Array element of did not give a value , Is to declare that the variable is not given a value , The default value is undefined
2 Add new array elements by modifying the array index
var arr = ['a','b','c'];
arr[3]='d';
console.log(arr);// Output a b c d
1.6 Flip array
var arr=['a','b','c'];
var new_Arr=[];
for(var i=arr.length-1;i>=0;i--){
new_Arr[new_Arr.length]=arr[i];
}
console.log(new_Arr);
1.7 Bubble sort
var arr=[1,2,6,5,19,4];
for(var i=0;i<arr.length;i++){
for(var j=0;j<=arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
var mid=arr[j];
arr[j]=arr[j+1];
arr[j+1]=mid;
}
}
}
console.log(arr);
2. function
function : It encapsulates a section of A block of code that can be repeatedly called and executed . A lot of code can be reused through this code block .
2.1 Use of functions
The function is used in two steps : Declare functions and Call function
1 Declare functions
function Function name (){
// Function body code
}
2 Call function
function Function name (){
// Function body code
}
Function name (); Execute the function body code by calling the function name
Be careful : Declaring the function itself does not execute code , Function body code is executed only when the function is called .
2.2 The parameters of the function
When you declare a function , You can add some parameters in parentheses after the function name , These parameters are called Shape parameter , And in the Call this function when , You also need to pass the corresponding parameters , These parameters are called Actual parameters .
Function of parameters : stay Internal function Some values cannot be fixed , We can use parameters in Pass different values when calling a function go in
function Function name ( Shape parameter 1, Shape parameter 2 , Shape parameter 3...) { // You can define as many parameters as you want , Separate with commas
// The body of the function
}
Function name ( Actual parameters 1, Actual parameters 2, Actual parameters 3...);
example Addition of two numbers
function sum(a,b){
alert(a+b);
}
sum(1,2);
Be careful : stay JavaScript in , The default value of a parameter is undefined
2.3 The return value of the function
sometimes , We would want the function to return the value to the caller , At this time, by using return Statement can be implemented .
return The syntax format of the statement is as follows :
// Declare functions
function Function name (){
...
return The value to be returned ;
}
// Call function
Function name (); // At this point, you can call the function to get the function body return Value after
In the use of return When the sentence is , Function will stop executing , And returns the specified value
If the function does not return , The value returned is undefined
- return The code after the statement is not executed
- return Only one value can be returned . If you separate multiple values with commas , Subject to the last one
Function calls another function
Because each function is a separate block of code , Used to complete special tasks , Therefore, it is often used when functions call each other .
2.4 arguments Use
When we are not sure how many parameters are passed , It can be used arguments To get . stay JavaScript in ,arguments In fact, it is a built-in object of the current function . All functions have a built-in arguments object ,arguments Object stores all the arguments passed .
arguments It stores the passed arguments
arguments The display form is a pseudo array , So you can traverse . Pseudo arrays have the following characteristics
- have length attribute
- Store data by index
- Without array push , pop Other methods
for example : Use the function to find the maximum value of any number
function maxNum() {
var max = arguments[0];
for (var i = 0; i < arguments.length; i++) {
if (max < arguments[i]) {
max = arguments[i];
}
}
return max;
}
console.log(maxNum(2, 4, 5, 9)); // 9
console.log(maxNum(12, 4, 9)); // 12
2.5 Two ways to declare functions
1 Name the function
// Declaration definition method
function Function name () {...}
// call
Function name ();
2 Anonymous functions
// This is a function expression , Anonymous functions end with a semicolon
var Variable name = function(){...};
// The way it's called , The function call must be written below the function body
Variable name ();
Because the function has no name , So it's also called Anonymous functions
This Variable name Stored inside is a function
The code of the function call must be written after the function body
边栏推荐
- MySQL——索引
- Ask and answer: dispel your doubts about the virtual function mechanism
- 《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!...
- MySQL——数据库备份
- 9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
- Inventory 2021 | yunyuansheng embracing the road
- The "boss management manual" that is wildly spread all over the network (turn)
- (5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan
- jvm jni 及 pvm pybind11 大批量数据传输及优化
- Memory analyzer (MAT)
猜你喜欢
The post-90s resigned and started a business, saying they would kill cloud database
Qt6 QML Book/Qt Quick 3D/基础知识
Après 90 ans, j'ai démissionné pour démarrer une entreprise et j'ai dit que j'allais détruire la base de données Cloud.
XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向
MySQL——索引
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
Inventory 2021 | yunyuansheng embracing the road
Gee calculated area
Software testing skills, JMeter stress testing tutorial, obtaining post request data in x-www-form-urlencoded format (24)
MySQL - index
随机推荐
Idea shortcut word operation
What if the Flink SQL client exits and the table is emptied?
JS three families
Advanced technology management - how to examine candidates in the interview and increase the entry probability
Yyds dry goods inventory TCP & UDP
2022 high voltage electrician examination and high voltage electrician reexamination examination
Inventory 2021 | yunyuansheng embracing the road
jvm jni 及 pvm pybind11 大批量数据传输及优化
仿网易云音乐小程序
MySQL - idea connects to MySQL
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析
2022-02-15 Daily: 2022 AAAI fellow release
使用dnSpy对无源码EXE或DLL进行反编译并且修改
The "boss management manual" that is wildly spread all over the network (turn)
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
Tidb's initial experience of ticdc6.0
设计电商秒杀系统
Basic preprocessing and data enhancement of image data
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!...
Single page application architecture