Object.prototype.looper = function() {
var result = [];
for (var property in this)
result.push(property);
return result;
};
var test = {name: "tab", age: 5};
console.log(test.looper());
如何消除Looper以获得钥匙
["name", "age", "looper", looper: function]
需要的输出
["name", "age"]
最佳答案
有两种解决方案。您可以使用Object.keys
来实现与looper
函数相同的功能:
var test = {name: "tab", age: 5};
console.log(Object.keys(test));
第二种方法是向
hasOwnProperty
添加其他检查looper
:Object.prototype.looper = function() {
var result = [];
for (var property in this)
if (this.hasOwnProperty(property))
result.push(property);
return result;
};
var test = {name: "tab", age: 5};
console.log(test.looper());
就这么简单。观看演示:
http://jsfiddle.net/aaditmshah/bhZbk/
http://jsfiddle.net/aaditmshah/bhZbk/1/
关于javascript - 从对象提取键时,对象具有额外的属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17783736/