.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
add-methods-after-inheritance.chinese.md
change-the-prototype-to-a-new-object.chinese.md
create-a-basic-javascript-object.chinese.md
create-a-method-on-an-object.chinese.md
define-a-constructor-function.chinese.md
extend-constructors-to-receive-arguments.chinese.md
inherit-behaviors-from-a-supertype.chinese.md
iterate-over-all-properties.chinese.md
make-code-more-reusable-with-the-this-keyword.chinese.md
override-inherited-methods.chinese.md
remember-to-set-the-constructor-property-when-changing-the-prototype.chinese.md
reset-an-inherited-constructor-property.chinese.md
set-the-childs-prototype-to-an-instance-of-the-parent.chinese.md
understand-own-properties.chinese.md
understand-the-constructor-property.chinese.md
understand-the-immediately-invoked-function-expression-iife.chinese.md
understand-the-prototype-chain.chinese.md
understand-where-an-objects-prototype-comes-from.chinese.md
use-a-constructor-to-create-objects.chinese.md
use-a-mixin-to-add-common-behavior-between-unrelated-objects.chinese.md
use-an-iife-to-create-a-module.chinese.md
use-closure-to-protect-properties-within-an-object-from-being-modified-externally.chinese.md
use-dot-notation-to-access-the-properties-of-an-object.chinese.md
use-inheritance-so-you-dont-repeat-yourself.chinese.md
use-prototype-properties-to-reduce-duplicate-code.chinese.md
verify-an-objects-constructor-with-instanceof.chinese.md
regular-expressions
03-front-end-libraries
04-data-visualization
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
english
portuguese
russian
spanish
formattingConversion
math-challenges
requiresTests
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
guide
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.french.md
README.italian.md
README.md
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
58 lines
2.3 KiB
Markdown
58 lines
2.3 KiB
Markdown
![]() |
---
|
|||
|
id: 587d7dae367417b2b2512b7a
|
|||
|
title: Verify an Object's Constructor with instanceof
|
|||
|
challengeType: 1
|
|||
|
videoUrl: ''
|
|||
|
localeTitle: 使用instanceof验证Object的构造函数
|
|||
|
---
|
|||
|
|
|||
|
## Description
|
|||
|
<section id="description">无论何时构造函数创建一个新对象,该对象都被称为其构造函数的一个<code>instance</code> 。 JavaScript提供了一种使用<code>instanceof</code>运算符验证这一点的便捷方法。 <code>instanceof</code>允许您将对象与构造函数进行比较,根据是否使用构造函数创建该对象,返回<code>true</code>或<code>false</code> 。这是一个例子: <blockquote>让Bird = function(名称,颜色){ <br> this.name = name; <br> this.color = color; <br> this.numLegs = 2; <br> } <br><br>让乌鸦=新鸟(“亚历克西斯”,“黑色”); <br><br>鸟的鸟; // =>是的</blockquote>如果在不使用构造函数的<code>instanceof</code>创建对象, <code>instanceof</code>将验证它不是该构造函数的实例: <blockquote>让金丝雀= { <br>名称:“Mildred”, <br>颜色:“黄色”, <br> numLegs:2 <br> }; <br><br>鸟类的金丝雀; // => false </blockquote></section>
|
|||
|
|
|||
|
## Instructions
|
|||
|
<section id="instructions">创建<code>House</code>构造函数的新实例,将其<code>myHouse</code>并传递多个卧室。然后,使用<code>instanceof</code>验证它是<code>House</code>的实例。 </section>
|
|||
|
|
|||
|
## Tests
|
|||
|
<section id='tests'>
|
|||
|
|
|||
|
```yml
|
|||
|
tests:
|
|||
|
- text: <code>myHouse</code>应该将<code>numBedrooms</code>属性设置为数字。
|
|||
|
testString: 'assert(typeof myHouse.numBedrooms === "number", "<code>myHouse</code> should have a <code>numBedrooms</code> attribute set to a number.");'
|
|||
|
- text: 请务必使用<code>instanceof</code>运算符验证<code>myHouse</code>是<code>House</code>的<code>instanceof</code> 。
|
|||
|
testString: 'assert(/myHouse\s*instanceof\s*House/.test(code), "Be sure to verify that <code>myHouse</code> is an instance of <code>House</code> using the <code>instanceof</code> operator.");'
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
</section>
|
|||
|
|
|||
|
## Challenge Seed
|
|||
|
<section id='challengeSeed'>
|
|||
|
|
|||
|
<div id='js-seed'>
|
|||
|
|
|||
|
```js
|
|||
|
/* jshint expr: true */
|
|||
|
|
|||
|
function House(numBedrooms) {
|
|||
|
this.numBedrooms = numBedrooms;
|
|||
|
}
|
|||
|
|
|||
|
// Add your code below this line
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</section>
|
|||
|
|
|||
|
## Solution
|
|||
|
<section id='solution'>
|
|||
|
|
|||
|
```js
|
|||
|
// solution required
|
|||
|
```
|
|||
|
</section>
|