for循环常被我们用来遍历数组,而如何遍历对象呢?
这时就需要用到for in循环了
写一个遍历对象名简写如下: for(var xxx in ooo){console.log(xxx)}
其中xxx是由用户自定名称,即指向每次遍历到的对象的指针
ooo是要遍历的对象,现把先前写的对象哪来遍历一下,代码
如下:
1 Person.prototype.Lname = ‘xiaohuang‘; 2 Person.prototype.modd = ‘unhappy‘; 3 function Person(name,age,sex){ 4 this.name = name ; 5 this.age = age ; 6 this.sex = sex; 7 } 8 var person = new Person(‘andi‘,18,‘man‘); 9 Student.prototype = person; 10 function Student(tel,grade){ 11 this.tel = tel; 12 this.grade = grade ; 13 } 14 var student = new Student(123,1); 15 for(var prop in student){ 16 console.log(prop); 17 }
成功输出了对象内属性名
原文地址:https://www.cnblogs.com/raonet/p/10354240.html
时间: 2024-11-05 18:35:37