freeCodeCamp/curriculum/challenges/italian/02-javascript-algorithms-an.../object-oriented-programming/understand-own-properties.md

2.1 KiB

id title challengeType forumTopicId dashedName
587d7dae367417b2b2512b7b Comprendere le proprietà proprie 1 301326 understand-own-properties

--description--

Nell'esempio seguente, il costruttore di Bird definisce due proprietà: name e numLegs:

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

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

name e numLegs sono chiamati proprietà proprie, perché sono definite direttamente nell'oggetto istanza. Questo significa che sia duck che canary hanno la propria copia separata di queste proprietà. Di fatto ogni istanza di Bird avrà la propria copia di queste proprietà. Il seguente codice aggiunge tutte le proprietà proprie di duck all'array ownProps:

let ownProps = [];

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

console.log(ownProps);

La console visualizzerà il valore ["name", "numLegs"].

--instructions--

Aggiungi le proprietà proprie di canary all'array ownProps.

--hints--

ownProps dovrebbe includere i valori numLegs e name.

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

Dovresti risolvere questa sfida senza usare il metodo integrato Object.keys().

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

Dovresti risolvere questa sfida senza predefinire il contenuto dell'array ownProps nel codice.

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);