当前位置:网站首页>循环遍历及函数基础知识
循环遍历及函数基础知识
2022-06-27 17:34:00 【编程奥特曼.】
for of 专门用来循环数组,或者类似数组结构,不能遍历普通对象,for of 是取每一条的数据
let arr = [1, 2, 3, 4]
// value 就是 for of 遍历的 数组的 数据
// let obj = {
// name:"雀雀",
// age:18
// }
let arr1 = []
for (let val of arr) {
// 循环length次
// value 就是 arr数组里的每一项
// 从arr里 拿出来的每一项 每次都 塞到 arr1里面去
// console.log(value);
arr1.push(val)
}
console.log(arr1);
for in 可以遍历对象或者数组(且多用于数组) 取属性值是key
//遍历对象
let obj ={
name:"雀雀",
age:18
}
for(let key in obj)
console.log(obj[key]); //雀雀 18
//遍历数组
let arr = [1,2,3,4]
for(let index in arr){
console.log(index); //取下标 0,1,2,3
}
console.log(arr); // [1,2,3,4]
while和do while的区别
while是先判断在决定执行不执行
do while 是先执行一次,在做判断
// while
// 计次 写在外面
let y = 0 //折叠次数
let num = 1 //纸的厚度
let hei = 8848000 //珠穆拉玛峰的高度
while(true){
//true的话 走进来
y++ //折叠一次
num *= 2 //折叠一次 纸的高度
if(num > hei){
console.log(y);
break;
}
}
//do while
let j = 1;
do{
console.log(j);
j++
}while(false){
}
何为函数?
函数就是将一系列代码或者逻辑打包到一起形成的就是函数,特定的时候,执行函数,复用代码
函数定义的方式分为声明函数和函数表达式
//函数声明
function add(){
}
add()
//函数表达式
let fn = function(){
// 写代码
console.log(1);
console.log("js太难了");
console.log("打死我也不学了");
}
fn()
函数的定义可以先使用后声明(变量提升,且不会报错),但最好还是先声明后使用
var声明的变量关键词也会变量提升,但是let就不会
函数一般不会存在数组里面,而是存在普通对象里面
let obj = {
fn3: function(){
console.log("fn3");}
}
console.log(obj.fn3);
函数表达式,立即执行函数(定义时,就马上使用)作用是主要创建一个单独的作用域,不会挂载到全局上去,和块作用域类似
(function(){
}())
(function(){
})()
+function(){
}()
function(){
}()
~function(){
}()
!function(){
}()
(function zhuque() {
//干扰js
let num = 10096
console.log(num); //10096
}())
!function(){
let num = 10086
console.log(num); //10086
}()
函数的参数
函数的参数分为形参和实参。形参命名时,且见名知意
形参写在函数定义时()内,其作用是用来接受实参传来的参数
实参写在执行函数时()内,其作用是用来给形参传递参数的
形参如果没有接受到实参的话 其形参为undefined
形参多于实参的时候,先按顺序一一执行,没有接受到的形参为undefined
实参多于形参的时候,先按顺序接收,多余的实参不管了
正常人的操作是你需要多少参数 就写多少形参和实参
function zhuque(value,value2){
console.log("朱雀真" + value + value2);
}
zhuque("美丽","可爱","大方")
zhuque("可爱")
zhuque("大方") // 朱雀真美丽可爱
// 朱雀真可爱undefined
// 朱雀真大方undefined
…rest是干什么的?
…rest是接受剩余参数的,必须要写在形参最后面,
function count(...rest){
// 参数 存到 rest 这个数组内
console.log(rest); // [1, 2, 3]
let num = 0
for(let value of rest){
// 每一次的叠加计算
num += value
}
console.log(num); //6
}
count(1,2,3)
如果…rest前面有形参接收,就先把rest前面的形参与实参一一对应,剩余的实参全给到rest
function count(a,...rest){
console.log(a); //1
console.log(rest); //[2, 100, 100, 100]
}
count(1,2,100,100,100)
…rest不一定非要写…rest,也可以命名其他的类型,都是可以接受的
function count(a,...arr){
console.log(a); //1
console.log(arr); //[2, 3]
}
count(1,2,3)
边栏推荐
- Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
- 芯动联科冲刺科创板:年营收1.7亿 北方电子院与中城创投是股东
- OpenSSL client programming: SSL session failure caused by an obscure function
- Making single test so simple -- initial experience of Spock framework
- 基于STM32F103ZET6库函数按键输入实验
- Market status and development prospect forecast of global functional polyethylene glycol (PEG) industry in 2022
- Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
- Seven phases of CMS implementation
- Bit. Store: long bear market, stable stacking products may become the main theme
- 云笔记到底哪家强 -- 教你搭建自己的网盘服务器
猜你喜欢

Erreur Keil de Huada Single Chip Computer La solution de Weak

Bit. Store: long bear market, stable stacking products may become the main theme

binder hwbinder vndbinder

GIS遥感R语言学习看这里

Two methods of MySQL database login and logout

全面解析零知识证明:消解扩容难题 重新定义「隐私安全」

China's Industrial Software Market Research Report is released, and SCADA and MES of force control enrich the ecology of domestic industrial software

电脑安全证书错误怎么处理比较好

International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding

爬取国家法律法规数据库
随机推荐
9.OpenFeign服务接口调用
Market status and development prospect forecast of global epoxy resin active toughener industry in 2022
Stored procedures of PostgreSQL
Blink SQL内置函数大全
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
NVIDIA Clara-AGX-Developer-Kit installation
Win10 LTSC 2021 wsappx CPU usage high
Keras deep learning practice (12) -- facial feature point detection
A simple calculation method of vanishing point
原创 | 2025实现“5个1”奋斗目标!解放动力全系自主非道路国四产品正式发布
Error reported by Huada MCU Keil_ Weak's solution
Bit. Store: long bear market, stable stacking products may become the main theme
昱琛航空IPO被终止:曾拟募资5亿 郭峥为大股东
Rxjs mergeMap 的使用场合
国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)
破解仓储难题?WMS仓储管理系统解决方案
Solution to Maxwell error (MySQL 8.x connection)
从感知机到前馈神经网络的数学推导
Common errors and solutions of MySQL reading binlog logs
信息学奥赛一本通 1333:【例2-2】Blah数集 | OpenJudge NOI 3.4 2729:Blah数集