当前位置:网站首页>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)
边栏推荐
- Crawler notes (3) -selenium and requests
- 百万年薪独家专访,开发人员不修复bug怎么办?
- 使用sqlite3语句后出现省略号 ... 的解决方法
- YOLOv6:又快又准的目标检测框架开源啦
- Go from introduction to actual combat - only any task is required to complete (notes)
- 使用Fiddler模拟弱网测试(2G/3G)
- Software test automation test -- interface test from entry to proficiency, learn a little every day
- Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
- Codeforces Round #719 (Div. 3)
- Secret script of test case design without leakage -- module test
猜你喜欢

爬虫笔记(2)- 解析

Crawler notes (3) -selenium and requests

结构化机器学习项目(二)- 机器学习策略(2)

美团20k软件测试工程师的经验分享

Management system itclub (medium)

Solution to the error of VMware tool plug-in installed in Windows 8.1 system
扁平数组和JSON树的转换

管理系統-ITclub(下)

Go language slice vs array panic: runtime error: index out of range problem solving

CUDA error:out of memory caused by insufficient video memory of 6G graphics card
随机推荐
Transformation from student to engineer
爬虫笔记(2)- 解析
Remote invocation of microservices
Codeforces Round #723 (Div. 2)
The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)
Use Fiddler to simulate weak network test (2g/3g)
Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string
Structured machine learning project (I) - machine learning strategy
真香,自从用了Charles,Fiddler已经被我彻底卸载了
使用Fiddler模拟弱网测试(2G/3G)
軟件測試自動化測試之——接口測試從入門到精通,每天學習一點點
VMware virtual machine PE startup
Gbase 8A OLAP analysis function cume_ Example of dist
QT large file generation MD5 check code
MONTHS_ Between function use
How to prioritize the contents in the queue every second
average-population-of-each-continent
Selenium上传文件有多少种方式?不信你有我全!
QT base64 encryption and decryption
Gbase 8A method for reducing the impact on the system by controlling resource usage through concurrency during node replacement of V8 version