Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
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.english.md
change-the-prototype-to-a-new-object.english.md
create-a-basic-javascript-object.english.md
create-a-method-on-an-object.english.md
define-a-constructor-function.english.md
extend-constructors-to-receive-arguments.english.md
inherit-behaviors-from-a-supertype.english.md
iterate-over-all-properties.english.md
make-code-more-reusable-with-the-this-keyword.english.md
override-inherited-methods.english.md
remember-to-set-the-constructor-property-when-changing-the-prototype.english.md
reset-an-inherited-constructor-property.english.md
set-the-childs-prototype-to-an-instance-of-the-parent.english.md
understand-own-properties.english.md
understand-the-constructor-property.english.md
understand-the-immediately-invoked-function-expression-iife.english.md
understand-the-prototype-chain.english.md
understand-where-an-objects-prototype-comes-from.english.md
use-a-constructor-to-create-objects.english.md
use-a-mixin-to-add-common-behavior-between-unrelated-objects.english.md
use-an-iife-to-create-a-module.english.md
use-closure-to-protect-properties-within-an-object-from-being-modified-externally.english.md
use-dot-notation-to-access-the-properties-of-an-object.english.md
use-inheritance-so-you-dont-repeat-yourself.english.md
use-prototype-properties-to-reduce-duplicate-code.english.md
verify-an-objects-constructor-with-instanceof.english.md
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
portuguese
russian
spanish
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
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
package-lock.json
package.json
sample.env

76 lines
1.9 KiB
Markdown
Raw Normal View History

---
id: 587d7dac367417b2b2512b73
title: Create a Basic JavaScript Object
challengeType: 1
isHidden: false
forumTopicId: 301317
---
## Description
<section id='description'>
Think about things people see every day, like cars, shops, and birds. These are all <dfn>objects</dfn>: tangible things people can observe and interact with.
What are some qualities of these objects? A car has wheels. Shops sell items. Birds have wings.
These qualities, or <dfn>properties</dfn>, define what makes up an object. Note that similar objects share the same properties, but may have different values for those properties. For example, all cars have wheels, but not all cars have the same number of wheels.
Objects in JavaScript are used to model real-world objects, giving them properties and behavior just like their real-world counterparts. Here's an example using these concepts to create a <code>duck</code> object:
```js
let duck = {
name: "Aflac",
numLegs: 2
};
```
This <code>duck</code> object has two property/value pairs: a <code>name</code> of "Aflac" and a <code>numLegs</code> of 2.
</section>
## Instructions
<section id='instructions'>
Create a <code>dog</code> object with <code>name</code> and <code>numLegs</code> properties, and set them to a string and a number, respectively.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>dog</code> should be an object.
testString: assert(typeof(dog) === 'object');
- text: <code>dog</code> should have a <code>name</code> property set to a <code>string</code>.
testString: assert(typeof(dog.name) === 'string');
- text: <code>dog</code> should have a <code>numLegs</code> property set to a <code>number</code>.
testString: assert(typeof(dog.numLegs) === 'number');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
let dog = {
};
```
</div>
</section>
## Solution
<section id='solution'>
```js
let dog = {
name: '',
numLegs: 4
};
```
</section>