* Use dfn tags * remove misused <dfn> tags * Revert "remove misused <dfn> tags" This reverts commit b24968a96810f618d831410ac90a0bc452ebde50. * Update curriculum/challenges/english/01-responsive-web-design/basic-html-and-html5/fill-in-the-blank-with-placeholder-text.english.md Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Make "array" lowercase Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Fix dfn usage * Address last dfn tags
2.0 KiB
2.0 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7dad367417b2b2512b78 | Use a Constructor to Create Objects | 1 | 18233 |
Description
Bird
constructor from the previous challenge:
function Bird() {
this.name = "Albert";
this.color = "blue";
this.numLegs = 2;
// "this" inside the constructor always refers to the object being created
}
let blueBird = new Bird();
Notice that the new
operator is used when calling a constructor. This tells JavaScript to create a new instance of Bird
called blueBird
. Without the new
operator, this
inside the constructor would not point to the newly created object, giving unexpected results.
Now blueBird
has all the properties defined inside the Bird
constructor:
blueBird.name; // => Albert
blueBird.color; // => blue
blueBird.numLegs; // => 2
Just like any other object, its properties can be accessed and modified:
blueBird.name = 'Elvira';
blueBird.name; // => Elvira
Instructions
Dog
constructor from the last lesson to create a new instance of Dog
, assigning it to a variable hound
.
Tests
tests:
- text: <code>hound</code> should be created using the <code>Dog</code> constructor.
testString: assert(hound instanceof Dog);
- text: Your code should use the <code>new</code> operator to create an instance of <code>Dog</code>.
testString: assert(code.match(/new/g));
Challenge Seed
function Dog() {
this.name = "Rupert";
this.color = "brown";
this.numLegs = 4;
}
// Add your code below this line
Solution
function Dog() {
this.name = "Rupert";
this.color = "brown";
this.numLegs = 4;
}
const hound = new Dog();