const myKeys = (obj)=>{ //定义返回值 const res = []; //遍历所有可枚举属性,推入res for (prop in obj){ //过滤掉原型上的属性用hasOwnProperty if(obj.hasOwnPoperty(prop)){ res.push(prop) } } return res } var arr = ['a', 'b', 'c']; console.log(Object.keys(arr)); // console: ['0', '1', '2'] // array like object with random key ordering var anObj = { 100: 'a', 2: 'b', 7: 'c' }; console.log(Object.keys(anObj)); // console: ['2', '7', '100']