码迷,mamicode.com
首页 > Web开发 > 详细

js中用for循环获取对象的变量

时间:2017-12-16 22:05:51      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:type   div   class   中括号   常量   typeof   循环   body   变量   

var person={
name:"ai",
age:"22"
};

for (var n in person){
console.log(n);
console.log(typeof n);
console.log(person.n);//常会思路是错的,这样他会找person.n即n这个属性,person中并不存在
console.log(person[n]);//上面那样是找常量,用中括号会找变量
}

js中用for循环获取对象的变量

标签:type   div   class   中括号   常量   typeof   循环   body   变量   

原文地址:http://www.cnblogs.com/Aizongbin/p/8047712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!