当前位置:网站首页>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)
边栏推荐
- How many ways does selenium upload files? I don't believe you have me all!
- It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
- 解决本地连接不上虚拟机的问题
- Software test automation test -- interface test from entry to proficiency, learn a little every day
- \w和[A-Za-z0-9_],\d和[0-9]等价吗?
- 年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数
- Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
- Golang uses regularity to match substring functions
- 《7天學會Go並發編程》第7天 go語言並發編程Atomic原子實戰操作含ABA問題
- Gbase 8A OLAP analysis function cume_ Example of dist
猜你喜欢

對話喬心昱:用戶是魏牌的產品經理,零焦慮定義豪華
![[MySQL] database function clearance Tutorial Part 2 (window function topic)](/img/03/2b37e63d0d482d5020b7421ac974cb.jpg)
[MySQL] database function clearance Tutorial Part 2 (window function topic)

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

渗透学习-sql注入过程中遇到的问题-针对sort=left(version(),1)的解释-对order by后接字符串的理解

年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

PCIe knowledge point -008: structure of PCIe switch

Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?

真香,自从用了Charles,Fiddler已经被我彻底卸载了

Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
随机推荐
Management system itclub (medium)
Penetration learning - shooting range chapter -dvwa shooting range detailed introduction (continuous updating - currently only the SQL injection part is updated)
Acwing week 57 longest continuous subsequence - (binary or tree array)
Macro task and micro task understanding
\w和[A-Za-z0-9_],\d和[0-9]等价吗?
北京邮电大学|用于成本和延迟敏感的虚拟网络功能放置和路由的多智能体深度强化学习
哈希表-数组之和
Yolov6: the fast and accurate target detection framework is open source
Structured machine learning project (I) - machine learning strategy
. Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method
Selenium上传文件有多少种方式?不信你有我全!
Codeforces Round #722 (Div. 2)
MONTHS_BETWEEN函数使用
How to design an elegant caching function
管理系统-ITclub(下)
Secret script of test case design without leakage -- module test
爬虫笔记(2)- 解析
How to prioritize the contents in the queue every second
QT base64 encryption and decryption
Common problems encountered by burp Suite