当前位置:网站首页>循环遍历及函数基础知识
循环遍历及函数基础知识
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)
边栏推荐
- Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
- Market status and development prospect forecast of global 4-methyl-2-pentanone industry in 2022
- 实施MES管理系统前,要对哪些问题进行评估
- Where to look at high-yield bank financial products?
- [notice of the Association] notice on holding summer special teacher training in the field of artificial intelligence and Internet of things
- 一位平凡毕业生的大学四年
- What is ICMP? What is the relationship between Ping and ICMP?
- 华大单片机KEIL报错_WEAK的解决方案
- Don't worry. This is the truth about wages in all industries in China
- 买股票在券商经理的开户链接上开户安全吗?求大神赐教
猜你喜欢

2022年第一季度消费金融APP用户洞察——总数达4479万人

9.OpenFeign服务接口调用

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

Two methods of MySQL database login and logout

昱琛航空IPO被终止:曾拟募资5亿 郭峥为大股东

Introduction to deep learning and neural networks

芯动联科冲刺科创板:年营收1.7亿 北方电子院与中城创投是股东

《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践

工作流自动化 低代码是关键

如何利用 RPA 实现自动化获客?
随机推荐
爬取国家法律法规数据库
xctf攻防世界 MISC薪手进阶区
Keras deep learning practice (12) -- facial feature point detection
Error reported by Huada MCU Keil_ Weak's solution
【云驻共创】高校数字化差旅建设“解决之道”
让单测变得如此简单 -- spock 框架初体验
如何利用 RPA 实现自动化获客?
Summary of domestic database certification test guide (updated on June 16, 2022)
New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million
Vs code runs "yarn run dev" and reports "yarn": the file XXX cannot be loaded
驾驭一切的垃圾收集器 -- G1
什么是 ICMP ?ping和ICMP之间有啥关系?
Solution to Maxwell error (MySQL 8.x connection)
binder hwbinder vndbinder
Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
深度学习和神经网络的介绍
Keras深度学习实战(12)——面部特征点检测
External interrupt experiment based on stm32f103zet6 library function
Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders
Market status and development prospect forecast of global epoxy resin active toughener industry in 2022