当前位置:网站首页>JS中for...of和for...in的区别
JS中for...of和for...in的区别
2022-07-27 05:03:00 【weixin_46051260】
JS中for…of和for…in的区别
for…in主要是为了遍历对象而产生,不适用于遍历数组,for…of循环可以用来遍历数组,类数组对象、字符串、、、
1)遍历对象
for…of遍历获取对象的键值,for…in获取对象的键名
for…in会遍历对象的整个原型链,for…of只遍历当前对象
2)数组的遍历
for…in返回数组中所有可枚举的属性,for…of返回数组的下表对应的属性值
function Person(name,age,sex){
this.name=name
this.age=age
this.sex=sex
}
Person.prototype.height=188
var p=new Person('张三',18,'男')
p[Symbol.iterator]=function(){
var keys=Object.keys(this)
var index=0
return {
next(){
if(index<keys.length){
return {
value:p[keys[index++]],done:false}
}
else{
return {
value:undefined,done:true}
}
}
}
}
for(let value of p){
console.log(value);// 张三 18 男
}
for (let value in p) {
console.log(value);//name age sex height
}
边栏推荐
- Flask对数据库的查询以及关联
- C语言函数入门介绍
- 后台品牌管理功能实现
- Notes series k8s orchestration MySQL container - stateful container creation process
- JS中apply、call、bind的区别
- Share a multiple-choice question about define (including the replacement rules, program environment and preprocessing related knowledge of define during precompiling)
- map结构
- flask项目配置
- 初识C语言——常见的数据类型
- 时间复杂度与空间复杂度
猜你喜欢

Li Hongyi machine learning team learning punch in activity day01 --- introduction to machine learning

初识C语言——初识指针

创建项目 实现登录注册,生成jwt,发送验证码

The concept of cloud native application and 15 characteristics of cloud native application

Flask请求数据获取与响应

后台品牌管理功能实现

Flask的使用

Sparse array → saving and continuation of Gobang

页面的基本布局

Li Hongyi machine learning team learning punch in activity day05 --- skills of network design
随机推荐
时间复杂度与空间复杂度
Source code of document type full-text retrieval knowledge base management system
封装JWT
GCC compilation options
Hi3516dv300 environment setup
Xiaomi mall project_ register
Carmaker quick start lesson 4 developing 48V P1 hybrid system
js轮播图
C语言函数入门介绍
异步数据-短信验证码
Redis publish subscribe mode
SQL database → constraint → design → multi table query → transaction
分享力扣—189.轮转数组 的三种解法
Day4 --- Flask 蓝图与Rest-ful
flask蓝图
Day6 --- SQLAlchemy进阶
图片上传的逻辑
Li Hongyi machine learning team learning punch in activity day02 --- return
p7 day1 初识Flask框架
[codeworks round 801 div2 D tree queries] tree greedy conclusion