Like the title
I don’t want to use jquery’s getOwnPropertyNames
var getProperty = function(obj) {
var nArr = [];
for (var i in obj) {
nArr.push[i];
}
console.log(nArr);
return nArr;
}
getProperty({a:1,b:2})
The final result returned is [];
if changed to
var getProperty = function(obj) {
var nArr = [],
k = 0;
for (var i in obj) {
nArr[k] = i;
k++;
}
console.log(nArr);
return nArr;
}
getProperty({a:1,b:2});
The correct result can be returned ['a','b'], why
nArr.push(i)
Wrong brackets! ! ! !
JS’s for in has a pitfall with hasOwnProperty.
You want to return
['a', 'b']
, just:That’s it (supports IE9+).
nArr.push[i]; Are you sure there will be no error when running this?