Files
2022-01-20 20:30:18 +01:00

2.4 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dae367417b2b2512b7b 独自のプロパティを理解する 1 301326 understand-own-properties

--description--

次の例では、Bird コンストラクターで namenumLegs の 2 つのプロパティを定義しています。

function Bird(name) {
  this.name = name;
  this.numLegs = 2;
}

let duck = new Bird("Donald");
let canary = new Bird("Tweety");

namenumLegs はインスタンスオブジェクトで直接定義されているため、独自のプロパティと呼ばれます。 つまり、duckcanary は、それぞれこれらのプロパティの独自のコピーを持つことになります。 実際、Bird のすべてのインスタンスは、これらのプロパティの独自のコピーを持ちます。 次のコードは、duck のすべての独自のプロパティを配列 ownProps に追加します。

let ownProps = [];

for (let property in duck) {
  if(duck.hasOwnProperty(property)) {
    ownProps.push(property);
  }
}

console.log(ownProps);

コンソールには値 ["name", "numLegs"] が表示されます。

--instructions--

canary の独自のプロパティを配列 ownProps に追加してください。

--hints--

ownProps には、numLegsname の値を含める必要があります。

assert(ownProps.indexOf('name') !== -1 && ownProps.indexOf('numLegs') !== -1);

このチャレンジは組み込みのメソッド Object.keys() を使用せずに解決する必要があります。

assert(!/Object(\.keys|\[(['"`])keys\2\])/.test(code));

ownProps 配列をハードコーディングせずに、このチャレンジを解決する必要があります。

assert(
  !/\[\s*(?:'|")(?:name|numLegs)|(?:push|concat)\(\s*(?:'|")(?:name|numLegs)/.test(
    code
  )
);

--seed--

--seed-contents--

function Bird(name) {
  this.name = name;
  this.numLegs = 2;
}

let canary = new Bird("Tweety");
let ownProps = [];
// Only change code below this line

--solutions--

function Bird(name) {
  this.name = name;
  this.numLegs = 2;
}

let canary = new Bird("Tweety");
function getOwnProps (obj) {
  const props = [];

  for (let prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      props.push(prop);
    }
  }

  return props;
}

const ownProps = getOwnProps(canary);