当前位置:网站首页>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
边栏推荐
- 内存分析器 (MAT)
- flink sql-client 退出,表就会被清空怎么办?
- Let me ask you a question. Have you ever used the asynchronous io of Flink SQL to associate dimension tables in MySQL? I set various settings according to the official website
- 技术管理进阶——如何在面试中考察候选人并增大入职概率
- MySQL——数据库备份
- MySQL——索引
- 一台服务器最大并发 tcp 连接数多少?65535?
- 【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
- QT6 QML book/qt quick 3d/ Basics
- Advanced collaboration: coroutinecontext
猜你喜欢

Mysql database ----- common commands of database (based on database)

Hcie security Day11: preliminarily learn the concepts of firewall dual machine hot standby and vgmp

强化学习-学习笔记1 | 基础概念

Discussion Net legacy application transformation

Advanced collaboration: coroutinecontext

leetcode-540. A single element in an ordered array

【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装

Such as the visual appeal of the live broadcast of NBA Finals, can you still see it like this?

Experience summary of database storage selection
![抓包整理外篇——————autoResponder、composer 、statistics [ 三]](/img/bf/ac3ba04c48e80b2d4f9c13894a4984.png)
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
随机推荐
Study diary: February 14th, 2022
The 12th Blue Bridge Cup
jvm jni 及 pvm pybind11 大批量数据传输及优化
Common SQL sets
[vulnhub shooting range] impulse: lupinone
Hcie security Day12: supplement the concept of packet filtering and security policy
Global and Chinese market of recycled yarn 2022-2028: Research Report on technology, participants, trends, market size and share
C程序设计的初步认识
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
上周内容回顾
Mysql - - Index
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
Ask and answer: dispel your doubts about the virtual function mechanism
Compilation Principle -- syntax analysis
设计电商秒杀系统
强化學習-學習筆記1 | 基礎概念
大神们,我想发两个广播流1 从mysql加载基础数据,广播出去2 从kafka加载基础数据的变更
Selenium has three waiting methods (forced waiting, implicit waiting, and display waiting)
Transformation between yaml, Jason and Dict
全网都在疯传的《老板管理手册》(转)