当前位置:网站首页>Basic knowledge of loop traversal and function
Basic knowledge of loop traversal and function
2022-06-27 22:31:00 【Program Altman】
for of Special for looping arrays , Or an array structure , Can't traverse ordinary objects ,for of Is to take each piece of data
let arr = [1, 2, 3, 4]
// value Namely for of Ergodic Array of data
// let obj = {
// name:" Finch ",
// age:18
// }
let arr1 = []
for (let val of arr) {
// loop length Time
// value Namely arr Every item in the array
// from arr in Take out every item Every time Plug into the arr1 Go inside
// console.log(value);
arr1.push(val)
}
console.log(arr1);
for in You can traverse objects or arrays ( And it is mostly used for arrays ) Get the attribute value yes key
// Traversing objects
let obj ={
name:" Finch ",
age:18
}
for(let key in obj)
console.log(obj[key]); // Finch 18
// Traversal array
let arr = [1,2,3,4]
for(let index in arr){
console.log(index); // Take the subscript 0,1,2,3
}
console.log(arr); // [1,2,3,4]
while and do while The difference between
while It is first to judge whether to execute or not
do while It's a first run , I'm making judgments
// while
// times count On the outside
let y = 0 // Fold times
let num = 1 // Thickness of paper
let hei = 8848000 // The height of zhumurama peak
while(true){
//true Words Come in
y++ // Fold once
num *= 2 // Fold once The height of the paper
if(num > hei){
console.log(y);
break;
}
}
//do while
let j = 1;
do{
console.log(j);
j++
}while(false){
}
What is a function ?
A function is a series of code or logic packed together to form a function , At certain times , Execute function , Code reuse
The methods of function definition are divided into declaration function and function expression
// Function declaration
function add(){
}
add()
// Function expression
let fn = function(){
// Write code
console.log(1);
console.log("js It's too hard ");
console.log(" Kill me and stop learning ");
}
fn()
Function definitions can be used first and then declared ( Variable Promotion , And will not report a mistake ), But it's better to declare before using
var Declared variable keywords also promote variables , however let Won't
Functions generally do not exist in arrays , It exists in ordinary objects
let obj = {
fn3: function(){
console.log("fn3");}
}
console.log(obj.fn3);
Function expression , Immediate execution function ( Definition time , Use immediately ) The scope is mainly to create a separate scope , It will not be attached to the global system , Similar to block scope
(function(){
}())
(function(){
})()
+function(){
}()
function(){
}()
~function(){
}()
!function(){
}()
(function zhuque() {
// interfere js
let num = 10096
console.log(num); //10096
}())
!function(){
let num = 10086
console.log(num); //10086
}()
The parameters of the function
The parameters of a function are divided into formal parameters and actual parameters . When naming formal parameters , See the name and know the meaning
Formal parameters are written in the function definition () Inside , Its function is to accept the parameters from the arguments
The arguments are written when the function is executed () Inside , Its function is to pass parameters to formal parameters
If a formal parameter does not receive an argument Its formal parameter is undefined
When there are more formal parameters than arguments , First, execute one by one in order , The formal parameter not received is undefined
When there are more arguments than formal parameters , Receive in sequence first , The extra arguments are ignored
The normal operation is how many parameters you need Just write as many formal and actual parameters
function zhuque(value,value2){
console.log(" Zhuquezhen " + value + value2);
}
zhuque(" beautiful "," lovely "," generous ")
zhuque(" lovely ")
zhuque(" generous ") // Rosefinch is so beautiful and lovely
// Rosefinch is so cute undefined
// Rosefinch is so generous undefined
…rest What is it ?
…rest Is to accept the remaining parameters , Must be written at the end of the formal parameter ,
function count(...rest){
// Parameters Deposit in rest In this array
console.log(rest); // [1, 2, 3]
let num = 0
for(let value of rest){
// Each iteration calculation
num += value
}
console.log(num); //6
}
count(1,2,3)
If …rest There is formal parameter reception in front , As the first rest The preceding shape participating arguments correspond to each other one by one , All the remaining arguments are given to rest
function count(a,...rest){
console.log(a); //1
console.log(rest); //[2, 100, 100, 100]
}
count(1,2,100,100,100)
…rest You don't have to write …rest, You can also name other types , All are acceptable
function count(a,...arr){
console.log(a); //1
console.log(arr); //[2, 3]
}
count(1,2,3)
边栏推荐
- QT large file generation MD5 check code
- Ellipsis after SQLite3 statement Solutions for
- 【mysql实战】查询语句实战演示
- 北京邮电大学|用于成本和延迟敏感的虚拟网络功能放置和路由的多智能体深度强化学习
- 清华大学教授:软件测试已经走入一个误区——“非代码不可”
- 正则表达式
- 【Redis】零基础十分钟学会Redis
- Dialogue with Qiao Xinyu: the user is the product manager of Wei brand, and zero anxiety defines luxury
- 信通院举办“业务与应用安全发展论坛” 天翼云安全能力再获认可
- 软件缺陷管理——测试人员必会
猜你喜欢

【Redis】零基础十分钟学会Redis

Dialogue with Qiao Xinyu: the user is the product manager of Wei brand, and zero anxiety defines luxury

average-population-of-each-continent

《7天學會Go並發編程》第7天 go語言並發編程Atomic原子實戰操作含ABA問題

Management system itclub (Part 2)

Figure countdownlatch and cyclicbarrier based on AQS queue

Experience sharing of meituan 20K Software Test Engineers

百万年薪独家专访,开发人员不修复bug怎么办?

6G显卡显存不足出现CUDA Error:out of memory解决办法

PCIe knowledge point -008: structure of PCIe switch
随机推荐
Software defect management - a must for testers
同花顺炒股软件可靠吗??安全嘛?
Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string
This set of steps for performance testing using JMeter includes two salary increases and one promotion
QT base64 encryption and decryption
Simulink method for exporting FMU model files
从学生到工程师的蜕变之路
Codeforces Round #719 (Div. 3)
Go language slice vs array panic: runtime error: index out of range problem solving
Gbase 8A OLAP analysis function cume_ Example of dist
Go from introduction to practice - error mechanism (note)
扁平数组和JSON树的转换
. Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method
6G显卡显存不足出现CUDA Error:out of memory解决办法
Remote invocation of microservices
Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)
Introduction to ARCS Model
Luogu p5706 redistributing fertilizer and house water
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
MONTHS_BETWEEN函数使用