[转载]for(variable in object)的对象类型 赋值问题
若为对象类型.
javascript会依次枚举对象的属性来执行循环,每次循环前;js都会先计算variable表达式的值,并将属性名(一个字符串)赋值给它.
例如:
var obj ={ type:'human', history:'20000years', use:'example' }; var arr=[],i=0; for(arr[i++] in obj); console.log(arr); //['type','history','use']
转载
// by 司徒正美 https://github.com/RubyLouvre/mass-Framework/blob/master/browers/lang.js var DONT_ENUM = "propertyIsEnumerable,isPrototypeOf,hasOwnProperty,toLocaleString,toString,valueOf,constructor".split(","), hasOwn = ({}).hasOwnProperty; for (var i in { toString: 1 }){ DONT_ENUM = false; } Object.keys = Object.keys || function(obj){//ecma262v5 15.2.3.14 var result = []; for(var key in obj ) if(hasOwn.call(obj,key)){ result.push(key) } if(DONT_ENUM && obj){ for(var i = 0 ;key = DONT_ENUM[i++]; ){ if(hasOwn.call(obj,key)){ result.push(key); } } } return result; };
并未看懂,日后来再读
Object.keys(obj),返回一个数组,数组里是该obj可被枚举的所有属性.
记住:是给Object.keys()传递一个对象参数obj,然后会返回该obj所有可被枚举的属性,如果该属性不可被枚举,则不会显示