JavaScript的for(item in object)循环

来源:互联网 发布:mac 退出less 编辑:程序博客网 时间:2024/05/24 07:44

基本写法

var obj = {    name: "崔秀英",    birth: "1990-02-10",    height: 170}for (var x in obj)    if (obj.hasOwnProperty(x))        console.log(obj[x]);

x对应obj对象属性名,若obj为数组,则x对应键名。

hasOwnProperty(x)方法用于判断obj[x]是否已被定义,从而过滤undefined内容。
PS:该判断可解决IDEA的Possible iteration over unexpected (custom / inherited) members, probably missing hasOwnProperty check警告。

0 0
原创粉丝点击