freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../object-oriented-programming/iterate-over-all-properties...

2.4 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7daf367417b2b2512b7d Iterate Over All Properties 1 迭代所有属性

Description

您现在已经看到了两种属性: own属性和prototype属性。 Own属性直接在对象实例本身上定义。和prototype属性所定义的prototype
function Birdname{
this.name = name; //拥有财产
}

Bird.prototype.numLegs = 2; //原型属性

让鸭子=新鸟(“唐纳德”);
以下是如何将duck own属性添加到数组ownPropsprototype属性到数组prototypeProps
让ownProps = [];
让prototypeProps = [];

forlet duck in duck{
ifduck.hasOwnPropertyproperty{
ownProps.push属性;
} else {
prototypeProps.push属性;
}
}

的console.logownProps; //打印[“名称”]
的console.logprototypeProps; //打印[“numLegs”]

Instructions

所有添加own的属性beagle到数组ownProps 。将Dog所有prototype属性添加到数组prototypeProps

Tests

tests:
  - text: <code>ownProps</code>数组应包含<code>&quot;name&quot;</code> 。
    testString: 'assert(ownProps.indexOf("name") !== -1, "The <code>ownProps</code> array should include <code>"name"</code>.");'
  - text: <code>prototypeProps</code>数组应该包含<code>&quot;numLegs&quot;</code> 。
    testString: 'assert(prototypeProps.indexOf("numLegs") !== -1, "The <code>prototypeProps</code> array should include <code>"numLegs"</code>.");'
  - text: 无需使用内置方法<code>Object.keys()</code>即可解决此挑战。
    testString: 'assert(!/\Object.keys/.test(code), "Solve this challenge without using the built in method <code>Object.keys()</code>.");'

Challenge Seed

function Dog(name) {
  this.name = name;
}

Dog.prototype.numLegs = 4;

let beagle = new Dog("Snoopy");

let ownProps = [];
let prototypeProps = [];

// Add your code below this line

Solution

// solution required