freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/testing-objects-for-propert...

1004 B

id title challengeType videoUrl forumTopicId
567af2437cbaa8c51670a16c 测试对象的属性 1 https://scrimba.com/c/cm8Q7Ua 18324

--description--

有时检查一个对象属性是否存在是非常有用的,我们可以用.hasOwnProperty(propname)方法来检查对象是否有该属性。如果有返回true,反之返回false

示例

var myObj = {
  top: "hat",
  bottom: "pants"
};
myObj.hasOwnProperty("top");    // true
myObj.hasOwnProperty("middle"); // false

--instructions--

修改函数checkObj检查myObj是否有checkProp属性,如果属性存在,返回属性对应的值,如果不存在,返回"Not Found"

--hints--

checkObj("gift")应该返回"pony"

assert(checkObj('gift') === 'pony');

checkObj("pet")应该返回"kitten"

assert(checkObj('pet') === 'kitten');

checkObj("house")应该返回"Not Found"

assert(checkObj('house') === 'Not Found');

--solutions--