1.8 KiB
1.8 KiB
id, challengeType, videoUrl, forumTopicId, localeTitle
id | challengeType | videoUrl | forumTopicId | localeTitle |
---|---|---|---|---|
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"
。
Tests
tests:
- text: <code>checkObj("gift")</code>应该返回<code>"pony"</code>。
testString: assert(checkObj("gift") === "pony");
- text: <code>checkObj("pet")</code>应该返回<code>"kitten"</code>。
testString: assert(checkObj("pet") === "kitten");
- text: <code>checkObj("house")</code>应该返回<code>"Not Found"</code>。
testString: assert(checkObj("house") === "Not Found");
Challenge Seed
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Your Code Here
return "Change Me!";
}
// Test your code by modifying these values
checkObj("gift");
Solution
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
} else {
return "Not Found";
}
}