找出对象 obj 不在原型链上的属性(注意这题测试例子的冒号后面也有一个空格~)
1、返回数组,格式为 key: value
2、结果数组不要求顺序
var C = function() {this.foo = 'bar'; this.baz = 'bim';}; C.prototype.bop = 'bip'; iterate(new C());
["foo: bar", "baz: bim"]
function iterate(obj) { let v = [] for(var i of Object.keys(obj)){ v.push(i+': '+obj[i]) } return v }
function iterate(obj) { var arr=[]; //Object.keys(obj)==> ['foo','baz']遍历 Object.keys(obj).forEach(function(item) { //拼键值 arr.push(item + ': ' + obj[item]) }) return arr; } var C = function() {this.foo = 'bar'; this.baz = 'bim';}; C.prototype.bop = 'bip'; iterate(new C());
function iterate(obj) { var result=Object.getOwnPropertyNames(obj); var length=result.length; for(var i=0;i<length;i++){ var name=result[i] result[i]=result[i]+": "+obj[name]; } return result; }
function iterate(obj) { return Object.keys(obj).map(function(key) { return key + ": " + obj[key]; }); }
function iterate(obj) { const res = []; for (var prop in obj) { if (obj.hasOwnProperty(prop)) { res.push(prop + ": " + obj[prop]); } } return res; }
function iterate(obj) { return Object.getOwnPropertyNames(obj).map(function(key) { return key + ": " + obj[key]; }); }
function iterate(obj) { var arr = Object.keys(obj); var res = []; arr.forEach( e => res.push(e+": "+obj[e])); return res; }
function iterate(obj) { return Object.getOwnPropertyNames(obj).map(e => { return e + ": " + obj[e] }); }