for in 和 for of 用法

for in示例

//for in 应用于数组

Array.prototype.sayHello = function(){

  console.log("Hello")

}

Array.prototype.str = 'world';

varmyArray = [1,2,10,30,100];

myArray.name='数组';


for(let index inmyArray){

  console.log(index);

}

//输出结果如下

0,1,2,3,4,name,str,sayHello


//for in 应用于对象中

Object.prototype.sayHello = function(){

  console.log('Hello');

}

Obeject.prototype.str = 'World';

varmyObject = {name:'zhangsan',age:100};


for(let index inmyObject){

  console.log(index);

}

//输出结果

name,age,str,sayHello

//首先输出的是对象的属性名,再是对象原型中的属性和方法,

//如果不想让其输出原型中的属性和方法,可以使用hasOwnProperty方法进行过滤

for(let index inmyObject){

  if(myObject.hasOwnProperty(index)){

    console.log(index)

  }

}

//输出结果为

name,age

//你也可以用Object.keys()方法获取所有的自身可枚举属性组成的数组。

Object.keys(myObject)


可以看出for in 应用于数组循环返回的是数组的下标和数组的属性和原型上的方法和属性,而for in应用于对象循环返回的是对象的属性名和原型中的方法和属性。

使用for in 也可以遍历数组,但是会存在以下问题:

1.index索引为字符串型数字,不能直接进行几何运算

2.遍历顺序有可能不是按照实际数组的内部顺序

3.使用for in会遍历数组所有的可枚举属性,包括原型。例如上栗的原型方法method和name属性


for of示例

Object.prototype.sayHello = function(){

  console.log('Hello');

}

varmyObject = {

  name:'zhangsan',

  age:10

}


for(let key of myObject){

  consoloe.log(key);

}

//输出结果

//typeError


Array.prototype.sayHello = function(){

  console.log("Hello");

}

varmyArray = [1,200,3,400,100];

for(let key of myArray){

  console.log(key);

}

//输出结果

1,200,3,400,100

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容