当前位置:网站首页>循环遍历及函数基础知识
循环遍历及函数基础知识
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)
边栏推荐
- PCB线路板蛇形布线要注意哪些问题?
- 9.OpenFeign服务接口调用
- 数仓的字符截取三胞胎:substrb、substr、substring
- ABAP随笔-EXCEL-3-批量导入(突破标准函数的9999行)
- 国信证券是国企吗?在国信证券开户资金安全吗?
- 数据分析师太火?月入3W?用数据告诉你这个行业的真实情况
- Current market situation and development prospect forecast of global 3,3 ', 4,4' - biphenyltetracarboxylic dianhydride industry in 2022
- 网络传输是怎么工作的 -- 详解 OSI 模型
- Online text batch inversion by line tool
- 实战回忆录:从Webshell开始突破边界
猜你喜欢

PCB线路板蛇形布线要注意哪些问题?

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

利用OpenCV执行相机校准

Open source summer 2022 | opengauss project selected and announced

明美新能源冲刺深交所:年应收账款超6亿 拟募资4.5亿

脉脉热帖:为啥大厂都热衷于造轮子?

External interrupt experiment based on stm32f103zet6 library function

Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders

New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million

如何利用 RPA 实现自动化获客?
随机推荐
Bit. Store: long bear market, stable stacking products may become the main theme
Market status and development prospect forecast of global active quality control air sampler industry in 2022
Usage of rxjs mergemap
Keras深度学习实战(12)——面部特征点检测
图扑数字孪生智慧能源一体化管控平台
买股票在券商经理的开户链接上开户安全吗?求大神赐教
NVIDIA Clara-AGX-Developer-Kit installation
Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
明美新能源冲刺深交所:年应收账款超6亿 拟募资4.5亿
基于STM32F103ZET6库函数蜂鸣器实验
NVIDIA Clara-AGX-Developer-Kit installation
让单测变得如此简单 -- spock 框架初体验
信息学奥赛一本通 1335:【例2-4】连通块
OpenSSL client programming: SSL session failure caused by an obscure function
如何实现IM即时通讯“消息”列表卡顿优化
PCB线路板蛇形布线要注意哪些问题?
Solution of adding st-link to Huada MCU Keil
im即时通讯开发之双进程守护保活实践
通过 G1 GC Log 重新认识 G1 垃圾回收器
Summary of domestic database certification test guide (updated on June 16, 2022)