我正在做 FreeCodeCamp javascript。我应该使用包含对象的所有自身属性的数组来获取输出。
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
let ownProps = canary.filter(prop => canary.hasOwnProperty(prop))
当我执行这个时,我得到:过滤器不是函数错误。
canary
这是Bird
一个对象的实例,对象没有任何调用的方法filter()
。如果要获取对象的所有属性,请使用Object.getOwnPropertyNames()
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
let ownProps = Object.getOwnPropertyNames(canary);
console.log(ownProps)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句