68 lines
2.1 KiB
Markdown
68 lines
2.1 KiB
Markdown
|
---
|
|||
|
id: 567af2437cbaa8c51670a16c
|
|||
|
title: Testing Objects for Properties
|
|||
|
challengeType: 1
|
|||
|
videoUrl: ''
|
|||
|
localeTitle: 测试属性的对象
|
|||
|
---
|
|||
|
|
|||
|
## Description
|
|||
|
<section id="description">有时检查给定对象的属性是否存在是有用的。我们可以使用对象的<code>.hasOwnProperty(propname)</code>方法来确定该对象是否具有给定的属性名称。 <code>.hasOwnProperty()</code>如果找到属性则返回<code>true</code>或<code>false</code> 。 <strong>例</strong> <blockquote> var myObj = { <br>顶部:“帽子”, <br>底部:“裤子” <br> }; <br> myObj.hasOwnProperty( “顶部”); //真的<br> myObj.hasOwnProperty( “中间”); //假</blockquote></section>
|
|||
|
|
|||
|
## Instructions
|
|||
|
<section id="instructions">修改函数<code>checkObj</code>以测试<code>myObj</code>的<code>checkProp</code> 。如果找到该属性,则返回该属性的值。如果没有,请返回<code>"Not Found"</code> 。 </section>
|
|||
|
|
|||
|
## Tests
|
|||
|
<section id='tests'>
|
|||
|
|
|||
|
```yml
|
|||
|
tests:
|
|||
|
- text: <code>checkObj("gift")</code>应该返回<code>"pony"</code> 。
|
|||
|
testString: 'assert(checkObj("gift") === "pony", "<code>checkObj("gift")</code> should return <code>"pony"</code>.");'
|
|||
|
- text: <code>checkObj("pet")</code>应该返回<code>"kitten"</code> 。
|
|||
|
testString: 'assert(checkObj("pet") === "kitten", "<code>checkObj("pet")</code> should return <code>"kitten"</code>.");'
|
|||
|
- text: <code>checkObj("house")</code>应该返回<code>"Not Found"</code> 。
|
|||
|
testString: 'assert(checkObj("house") === "Not Found", "<code>checkObj("house")</code> should return <code>"Not Found"</code>.");'
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
</section>
|
|||
|
|
|||
|
## Challenge Seed
|
|||
|
<section id='challengeSeed'>
|
|||
|
|
|||
|
<div id='js-seed'>
|
|||
|
|
|||
|
```js
|
|||
|
// 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");
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</section>
|
|||
|
|
|||
|
## Solution
|
|||
|
<section id='solution'>
|
|||
|
|
|||
|
```js
|
|||
|
// solution required
|
|||
|
```
|
|||
|
</section>
|