chore: import upstream snapshot with attribution
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
This commit is contained in:
+69
@@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 587d7dac367417b2b2512b73
|
||||
title: Create a Basic JavaScript Object
|
||||
challengeType: 1
|
||||
forumTopicId: 301317
|
||||
dashedName: create-a-basic-javascript-object
|
||||
---
|
||||
|
||||
# --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 `duck` object:
|
||||
|
||||
```js
|
||||
let duck = {
|
||||
name: "Aflac",
|
||||
numLegs: 2
|
||||
};
|
||||
```
|
||||
|
||||
This `duck` object has two property/value pairs: a `name` of `Aflac` and a `numLegs` of 2.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create a `dog` object with `name` and `numLegs` properties, and set them to a string and a number, respectively.
|
||||
|
||||
# --hints--
|
||||
|
||||
`dog` should be an object.
|
||||
|
||||
```js
|
||||
assert(typeof dog === 'object');
|
||||
```
|
||||
|
||||
`dog` should have a `name` property set to a string.
|
||||
|
||||
```js
|
||||
assert(typeof dog.name === 'string');
|
||||
```
|
||||
|
||||
`dog` should have a `numLegs` property set to a number.
|
||||
|
||||
```js
|
||||
assert(typeof dog.numLegs === 'number');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let dog = {
|
||||
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let dog = {
|
||||
name: '',
|
||||
numLegs: 4
|
||||
};
|
||||
```
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
---
|
||||
id: 587d7dac367417b2b2512b74
|
||||
title: Use Dot Notation to Access the Properties of an Object
|
||||
challengeType: 1
|
||||
forumTopicId: 301333
|
||||
dashedName: use-dot-notation-to-access-the-properties-of-an-object
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The last challenge created an object with various properties. Now you'll see how to access the values of those properties. Here's an example:
|
||||
|
||||
```js
|
||||
let duck = {
|
||||
name: "Aflac",
|
||||
numLegs: 2
|
||||
};
|
||||
console.log(duck.name);
|
||||
```
|
||||
|
||||
Dot notation is used on the object name, `duck`, followed by the name of the property, `name`, to access the value of `Aflac`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Print both properties of the `dog` object to your console.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your code should use `console.log` to print the value for the `name` property of the `dog` object.
|
||||
|
||||
```js
|
||||
assert(/console.log\(.*dog\.name.*\)/g.test(__helpers.removeJSComments(code)));
|
||||
```
|
||||
|
||||
Your code should use `console.log` to print the value for the `numLegs` property of the `dog` object.
|
||||
|
||||
```js
|
||||
assert(/console.log\(.*dog\.numLegs.*\)/g.test(__helpers.removeJSComments(code)));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let dog = {
|
||||
name: "Spot",
|
||||
numLegs: 4
|
||||
};
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let dog = {
|
||||
name: "Spot",
|
||||
numLegs: 4
|
||||
};
|
||||
console.log(dog.name);
|
||||
console.log(dog.numLegs);
|
||||
```
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: 587d7dad367417b2b2512b75
|
||||
title: Create a Method on an Object
|
||||
challengeType: 1
|
||||
forumTopicId: 301318
|
||||
dashedName: create-a-method-on-an-object
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Objects can have a special type of property, called a <dfn>method</dfn>.
|
||||
|
||||
Methods are properties that are functions. This adds different behavior to an object. Here is the `duck` example with a method:
|
||||
|
||||
```js
|
||||
let duck = {
|
||||
name: "Aflac",
|
||||
numLegs: 2,
|
||||
sayName: function() {return "The name of this duck is " + duck.name + ".";}
|
||||
};
|
||||
duck.sayName();
|
||||
```
|
||||
|
||||
The example adds the `sayName` method, which is a function that returns a sentence giving the name of the `duck`. Notice that the method accessed the `name` property in the return statement using `duck.name`. The next challenge will cover another way to do this.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Using the `dog` object, give it a method called `sayLegs`. The method should return the sentence `This dog has 4 legs.`
|
||||
|
||||
# --hints--
|
||||
|
||||
`dog.sayLegs()` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof dog.sayLegs === 'function');
|
||||
```
|
||||
|
||||
`dog.sayLegs()` should return the given string - note that punctuation and spacing matter.
|
||||
|
||||
```js
|
||||
assert(dog.sayLegs() === 'This dog has 4 legs.');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let dog = {
|
||||
name: "Spot",
|
||||
numLegs: 4,
|
||||
|
||||
};
|
||||
|
||||
dog.sayLegs();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let dog = {
|
||||
name: "Spot",
|
||||
numLegs: 4,
|
||||
sayLegs () {
|
||||
return 'This dog has ' + this.numLegs + ' legs.';
|
||||
}
|
||||
};
|
||||
|
||||
dog.sayLegs();
|
||||
```
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 587d7dad367417b2b2512b76
|
||||
title: Make Code More Reusable with the this Keyword
|
||||
challengeType: 1
|
||||
forumTopicId: 301321
|
||||
dashedName: make-code-more-reusable-with-the-this-keyword
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The last challenge introduced a method to the `duck` object. It used `duck.name` dot notation to access the value for the `name` property within the return statement:
|
||||
|
||||
```js
|
||||
sayName: function() {return "The name of this duck is " + duck.name + ".";}
|
||||
```
|
||||
|
||||
While this is a valid way to access the object's property, there is a pitfall here. If the variable name changes, any code referencing the original name would need to be updated as well. In a short object definition, it isn't a problem, but if an object has many references to its properties there is a greater chance for error.
|
||||
|
||||
A way to avoid these issues is with the `this` keyword:
|
||||
|
||||
```js
|
||||
let duck = {
|
||||
name: "Aflac",
|
||||
numLegs: 2,
|
||||
sayName: function() {return "The name of this duck is " + this.name + ".";}
|
||||
};
|
||||
```
|
||||
|
||||
`this` is a deep topic, and the above example is only one way to use it. In the current context, `this` refers to the object that the method is associated with: `duck`. If the object's name is changed to `mallard`, it is not necessary to find all the references to `duck` in the code. It makes the code reusable and easier to read.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Modify the `dog.sayLegs` method to remove any references to `dog`. Use the `duck` example for guidance.
|
||||
|
||||
# --hints--
|
||||
|
||||
`dog.sayLegs()` should return the given string.
|
||||
|
||||
```js
|
||||
assert(dog.sayLegs() === 'This dog has 4 legs.');
|
||||
```
|
||||
|
||||
Your code should use the `this` keyword to access the `numLegs` property of `dog`.
|
||||
|
||||
```js
|
||||
assert(__helpers.removeJSComments(code).match(/this\.numLegs/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let dog = {
|
||||
name: "Spot",
|
||||
numLegs: 4,
|
||||
sayLegs: function() {return "This dog has " + dog.numLegs + " legs.";}
|
||||
};
|
||||
|
||||
dog.sayLegs();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let dog = {
|
||||
name: "Spot",
|
||||
numLegs: 4,
|
||||
sayLegs () {
|
||||
return 'This dog has ' + this.numLegs + ' legs.';
|
||||
}
|
||||
};
|
||||
|
||||
dog.sayLegs();
|
||||
```
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 587d7dad367417b2b2512b77
|
||||
title: Define a Constructor Function
|
||||
challengeType: 1
|
||||
forumTopicId: 16804
|
||||
dashedName: define-a-constructor-function
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<dfn>Constructors</dfn> are functions that create new objects. They define properties and behaviors that will belong to the new object. Think of them as a blueprint for the creation of new objects.
|
||||
|
||||
Here is an example of a constructor:
|
||||
|
||||
```js
|
||||
function Bird() {
|
||||
this.name = "Albert";
|
||||
this.color = "blue";
|
||||
this.numLegs = 2;
|
||||
}
|
||||
```
|
||||
|
||||
This constructor defines a `Bird` object with properties `name`, `color`, and `numLegs` set to Albert, blue, and 2, respectively. Constructors follow a few conventions:
|
||||
|
||||
<ul><li>Constructors are defined with a capitalized name to distinguish them from other functions that are not <code>constructors</code>.</li><li>Constructors use the keyword <code>this</code> to set properties of the object they will create. Inside the constructor, <code>this</code> refers to the new object it will create.</li><li>Constructors define properties and behaviors instead of returning a value as other functions might.</li></ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create a constructor, `Dog`, with properties `name`, `color`, and `numLegs` that are set to a string, a string, and a number, respectively.
|
||||
|
||||
# --hints--
|
||||
|
||||
`Dog` should have a `name` property set to a string.
|
||||
|
||||
```js
|
||||
assert(typeof new Dog().name === 'string');
|
||||
```
|
||||
|
||||
`Dog` should have a `color` property set to a string.
|
||||
|
||||
```js
|
||||
assert(typeof new Dog().color === 'string');
|
||||
```
|
||||
|
||||
`Dog` should have a `numLegs` property set to a number.
|
||||
|
||||
```js
|
||||
assert(typeof new Dog().numLegs === 'number');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog (name, color, numLegs) {
|
||||
this.name = 'name';
|
||||
this.color = 'color';
|
||||
this.numLegs = 4;
|
||||
}
|
||||
```
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 587d7dad367417b2b2512b78
|
||||
title: Use a Constructor to Create Objects
|
||||
challengeType: 1
|
||||
forumTopicId: 18233
|
||||
dashedName: use-a-constructor-to-create-objects
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Here's the `Bird` constructor from the previous challenge:
|
||||
|
||||
```js
|
||||
function Bird() {
|
||||
this.name = "Albert";
|
||||
this.color = "blue";
|
||||
this.numLegs = 2;
|
||||
}
|
||||
|
||||
let blueBird = new Bird();
|
||||
```
|
||||
|
||||
**NOTE:** `this` inside the constructor always refers to the object being created.
|
||||
|
||||
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:
|
||||
|
||||
```js
|
||||
blueBird.name;
|
||||
blueBird.color;
|
||||
blueBird.numLegs;
|
||||
```
|
||||
|
||||
Just like any other object, its properties can be accessed and modified:
|
||||
|
||||
```js
|
||||
blueBird.name = 'Elvira';
|
||||
blueBird.name;
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the `Dog` constructor from the last lesson to create a new instance of `Dog`, assigning it to a variable `hound`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`hound` should be created using the `Dog` constructor.
|
||||
|
||||
```js
|
||||
assert(hound instanceof Dog);
|
||||
```
|
||||
|
||||
Your code should use the `new` operator to create an instance of `Dog`.
|
||||
|
||||
```js
|
||||
assert(__helpers.removeJSComments(code).match(/new/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Dog() {
|
||||
this.name = "Rupert";
|
||||
this.color = "brown";
|
||||
this.numLegs = 4;
|
||||
}
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog() {
|
||||
this.name = "Rupert";
|
||||
this.color = "brown";
|
||||
this.numLegs = 4;
|
||||
}
|
||||
const hound = new Dog();
|
||||
```
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 587d7dae367417b2b2512b79
|
||||
title: Extend Constructors to Receive Arguments
|
||||
challengeType: 1
|
||||
forumTopicId: 18235
|
||||
dashedName: extend-constructors-to-receive-arguments
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The `Bird` and `Dog` constructors from the last challenge worked well. However, notice that all `Birds` that are created with the `Bird` constructor are automatically named Albert, are blue in color, and have two legs. What if you want birds with different values for name and color? It's possible to change the properties of each bird manually but that would be a lot of work:
|
||||
|
||||
```js
|
||||
let swan = new Bird();
|
||||
swan.name = "Carlos";
|
||||
swan.color = "white";
|
||||
```
|
||||
|
||||
Suppose you were writing a program to keep track of hundreds or even thousands of different birds in an aviary. It would take a lot of time to create all the birds, then change the properties to different values for every one. To more easily create different `Bird` objects, you can design your Bird constructor to accept parameters:
|
||||
|
||||
```js
|
||||
function Bird(name, color) {
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
this.numLegs = 2;
|
||||
}
|
||||
```
|
||||
|
||||
Then pass in the values as arguments to define each unique bird into the `Bird` constructor: `let cardinal = new Bird("Bruce", "red");` This gives a new instance of `Bird` with `name` and `color` properties set to `Bruce` and `red`, respectively. The `numLegs` property is still set to 2. The `cardinal` has these properties:
|
||||
|
||||
```js
|
||||
cardinal.name
|
||||
cardinal.color
|
||||
cardinal.numLegs
|
||||
```
|
||||
|
||||
The constructor is more flexible. It's now possible to define the properties for each `Bird` at the time it is created, which is one way that JavaScript constructors are so useful. They group objects together based on shared characteristics and behavior and define a blueprint that automates their creation.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create another `Dog` constructor. This time, set it up to take the parameters `name` and `color`, and have the property `numLegs` fixed at 4. Then create a new `Dog` saved in a variable `terrier`. Pass it two strings as arguments for the `name` and `color` properties.
|
||||
|
||||
# --hints--
|
||||
|
||||
`Dog` should receive an argument for `name`.
|
||||
|
||||
```js
|
||||
assert(new Dog('Clifford').name === 'Clifford');
|
||||
```
|
||||
|
||||
`Dog` should receive an argument for `color`.
|
||||
|
||||
```js
|
||||
assert(new Dog('Clifford', 'yellow').color === 'yellow');
|
||||
```
|
||||
|
||||
`Dog` should have property `numLegs` set to 4.
|
||||
|
||||
```js
|
||||
assert(new Dog('Clifford').numLegs === 4);
|
||||
```
|
||||
|
||||
`terrier` should be created using the `Dog` constructor.
|
||||
|
||||
```js
|
||||
assert(terrier instanceof Dog);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Dog() {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog (name, color) {
|
||||
this.numLegs = 4;
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
const terrier = new Dog();
|
||||
```
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 587d7dae367417b2b2512b7a
|
||||
title: Verify an Object's Constructor with instanceof
|
||||
challengeType: 1
|
||||
forumTopicId: 301337
|
||||
dashedName: verify-an-objects-constructor-with-instanceof
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Anytime a constructor function creates a new object, that object is said to be an <dfn>instance</dfn> of its constructor. JavaScript gives a convenient way to verify this with the `instanceof` operator. `instanceof` allows you to compare an object to a constructor, returning `true` or `false` based on whether or not that object was created with the constructor. Here's an example:
|
||||
|
||||
```js
|
||||
let Bird = function(name, color) {
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
this.numLegs = 2;
|
||||
}
|
||||
|
||||
let crow = new Bird("Alexis", "black");
|
||||
|
||||
crow instanceof Bird;
|
||||
```
|
||||
|
||||
This `instanceof` method would return `true`.
|
||||
|
||||
If an object is created without using a constructor, `instanceof` will verify that it is not an instance of that constructor:
|
||||
|
||||
```js
|
||||
let canary = {
|
||||
name: "Mildred",
|
||||
color: "Yellow",
|
||||
numLegs: 2
|
||||
};
|
||||
|
||||
canary instanceof Bird;
|
||||
```
|
||||
|
||||
This `instanceof` method would return `false`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create a new instance of the `House` constructor, calling it `myHouse` and passing a number of bedrooms. Then, use `instanceof` to verify that it is an instance of `House`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`myHouse` should have a `numBedrooms` attribute set to a number.
|
||||
|
||||
```js
|
||||
assert(typeof myHouse.numBedrooms === 'number');
|
||||
```
|
||||
|
||||
You should verify that `myHouse` is an instance of `House` using the `instanceof` operator.
|
||||
|
||||
```js
|
||||
assert(/myHouse\s*instanceof\s*House/.test(__helpers.removeJSComments(code)));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function House(numBedrooms) {
|
||||
this.numBedrooms = numBedrooms;
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function House(numBedrooms) {
|
||||
this.numBedrooms = numBedrooms;
|
||||
}
|
||||
const myHouse = new House(4);
|
||||
console.log(myHouse instanceof House);
|
||||
```
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
---
|
||||
id: 587d7dae367417b2b2512b7b
|
||||
title: Understand Own Properties
|
||||
challengeType: 1
|
||||
forumTopicId: 301326
|
||||
dashedName: understand-own-properties
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the following example, the `Bird` constructor defines two properties: `name` and `numLegs`:
|
||||
|
||||
```js
|
||||
function Bird(name) {
|
||||
this.name = name;
|
||||
this.numLegs = 2;
|
||||
}
|
||||
|
||||
let duck = new Bird("Donald");
|
||||
let canary = new Bird("Tweety");
|
||||
```
|
||||
|
||||
`name` and `numLegs` are called <dfn>own properties</dfn>, because they are defined directly on the instance object. That means that `duck` and `canary` each has its own separate copy of these properties. In fact every instance of `Bird` will have its own copy of these properties. The following code adds all of the own properties of `duck` to the array `ownProps`:
|
||||
|
||||
```js
|
||||
let ownProps = [];
|
||||
|
||||
for (let property in duck) {
|
||||
if(duck.hasOwnProperty(property)) {
|
||||
ownProps.push(property);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(ownProps);
|
||||
```
|
||||
|
||||
The console would display the value `["name", "numLegs"]`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Add the own properties of `canary` to the array `ownProps`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`ownProps` should include the values `numLegs` and `name`.
|
||||
|
||||
```js
|
||||
assert(ownProps.indexOf('name') !== -1 && ownProps.indexOf('numLegs') !== -1);
|
||||
```
|
||||
|
||||
You should solve this challenge without using the built in method `Object.keys()`.
|
||||
|
||||
```js
|
||||
assert(!/Object(\.keys|\[(['"`])keys\2\])/.test(__helpers.removeJSComments(code)));
|
||||
```
|
||||
|
||||
You should solve this challenge without hardcoding the `ownProps` array.
|
||||
|
||||
```js
|
||||
assert(
|
||||
!/\[\s*(?:'|")(?:name|numLegs)|(?:push|concat)\(\s*(?:'|")(?:name|numLegs)/.test(
|
||||
__helpers.removeJSComments(code)
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Bird(name) {
|
||||
this.name = name;
|
||||
this.numLegs = 2;
|
||||
}
|
||||
|
||||
let canary = new Bird("Tweety");
|
||||
let ownProps = [];
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Bird(name) {
|
||||
this.name = name;
|
||||
this.numLegs = 2;
|
||||
}
|
||||
|
||||
let canary = new Bird("Tweety");
|
||||
function getOwnProps (obj) {
|
||||
const props = [];
|
||||
|
||||
for (let prop in obj) {
|
||||
if (obj.hasOwnProperty(prop)) {
|
||||
props.push(prop);
|
||||
}
|
||||
}
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
const ownProps = getOwnProps(canary);
|
||||
```
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 587d7dae367417b2b2512b7c
|
||||
title: Use Prototype Properties to Reduce Duplicate Code
|
||||
challengeType: 1
|
||||
forumTopicId: 301336
|
||||
dashedName: use-prototype-properties-to-reduce-duplicate-code
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Since `numLegs` will probably have the same value for all instances of `Bird`, you essentially have a duplicated variable `numLegs` inside each `Bird` instance.
|
||||
|
||||
This may not be an issue when there are only two instances, but imagine if there are millions of instances. That would be a lot of duplicated variables.
|
||||
|
||||
A better way is to use the `prototype` of `Bird`. Properties in the `prototype` are shared among ALL instances of `Bird`. Here's how to add `numLegs` to the `Bird prototype`:
|
||||
|
||||
```js
|
||||
Bird.prototype.numLegs = 2;
|
||||
```
|
||||
|
||||
Now all instances of `Bird` have the `numLegs` property.
|
||||
|
||||
```js
|
||||
console.log(duck.numLegs);
|
||||
console.log(canary.numLegs);
|
||||
```
|
||||
|
||||
Since all instances automatically have the properties on the `prototype`, think of a `prototype` as a "recipe" for creating objects. Note that the `prototype` for `duck` and `canary` is part of the `Bird` constructor as `Bird.prototype`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Add a `numLegs` property to the `prototype` of `Dog`
|
||||
|
||||
# --hints--
|
||||
|
||||
`beagle` should have a `numLegs` property.
|
||||
|
||||
```js
|
||||
assert(beagle.numLegs !== undefined);
|
||||
```
|
||||
|
||||
`beagle.numLegs` should be a number.
|
||||
|
||||
```js
|
||||
assert(typeof beagle.numLegs === 'number');
|
||||
```
|
||||
|
||||
`numLegs` should be a `prototype` property not an own property.
|
||||
|
||||
```js
|
||||
assert(beagle.hasOwnProperty('numLegs') === false);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
let beagle = new Dog("Snoopy");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog (name) {
|
||||
this.name = name;
|
||||
}
|
||||
Dog.prototype.numLegs = 4;
|
||||
let beagle = new Dog("Snoopy");
|
||||
```
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
---
|
||||
id: 587d7daf367417b2b2512b7d
|
||||
title: Iterate Over All Properties
|
||||
challengeType: 1
|
||||
forumTopicId: 301320
|
||||
dashedName: iterate-over-all-properties
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
You have now seen two kinds of properties: <dfn>own properties</dfn> and `prototype` properties. Own properties are defined directly on the object instance itself. And prototype properties are defined on the `prototype`.
|
||||
|
||||
```js
|
||||
function Bird(name) {
|
||||
this.name = name; //own property
|
||||
}
|
||||
|
||||
Bird.prototype.numLegs = 2; // prototype property
|
||||
|
||||
let duck = new Bird("Donald");
|
||||
```
|
||||
|
||||
Here is how you add `duck`'s own properties to the array `ownProps` and `prototype` properties to the array `prototypeProps`:
|
||||
|
||||
```js
|
||||
let ownProps = [];
|
||||
let prototypeProps = [];
|
||||
|
||||
for (let property in duck) {
|
||||
if(duck.hasOwnProperty(property)) {
|
||||
ownProps.push(property);
|
||||
} else {
|
||||
prototypeProps.push(property);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(ownProps);
|
||||
console.log(prototypeProps);
|
||||
```
|
||||
|
||||
`console.log(ownProps)` would display `["name"]` in the console, and `console.log(prototypeProps)` would display `["numLegs"]`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Add all of the own properties of `beagle` to the array `ownProps`. Add all of the `prototype` properties of `Dog` to the array `prototypeProps`.
|
||||
|
||||
# --hints--
|
||||
|
||||
The `ownProps` array should only contain `name`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(ownProps, ['name']);
|
||||
```
|
||||
|
||||
The `prototypeProps` array should only contain `numLegs`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(prototypeProps, ['numLegs']);
|
||||
```
|
||||
|
||||
You should solve this challenge without using the built in method `Object.keys()`.
|
||||
|
||||
```js
|
||||
assert(!/\Object.keys/.test(__helpers.removeJSComments(code)));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
Dog.prototype.numLegs = 4;
|
||||
|
||||
let beagle = new Dog("Snoopy");
|
||||
|
||||
let ownProps = [];
|
||||
let prototypeProps = [];
|
||||
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
Dog.prototype.numLegs = 4;
|
||||
|
||||
let beagle = new Dog("Snoopy");
|
||||
|
||||
let ownProps = [];
|
||||
let prototypeProps = [];
|
||||
for (let prop in beagle) {
|
||||
if (beagle.hasOwnProperty(prop)) {
|
||||
ownProps.push(prop);
|
||||
} else {
|
||||
prototypeProps.push(prop);
|
||||
}
|
||||
}
|
||||
```
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
---
|
||||
id: 587d7daf367417b2b2512b7e
|
||||
title: Understand the Constructor Property
|
||||
challengeType: 1
|
||||
forumTopicId: 301327
|
||||
dashedName: understand-the-constructor-property
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
There is a special `constructor` property located on the object instances `duck` and `beagle` that were created in the previous challenges:
|
||||
|
||||
```js
|
||||
let duck = new Bird();
|
||||
let beagle = new Dog();
|
||||
|
||||
console.log(duck.constructor === Bird);
|
||||
console.log(beagle.constructor === Dog);
|
||||
```
|
||||
|
||||
Both of these `console.log` calls would display `true` in the console.
|
||||
|
||||
Note that the `constructor` property is a reference to the constructor function that created the instance. The advantage of the `constructor` property is that it's possible to check for this property to find out what kind of object it is. Here's an example of how this could be used:
|
||||
|
||||
```js
|
||||
function joinBirdFraternity(candidate) {
|
||||
if (candidate.constructor === Bird) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Note:** Since the `constructor` property can be overwritten (which will be covered in the next two challenges) it’s generally better to use the `instanceof` method to check the type of an object.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a `joinDogFraternity` function that takes a `candidate` parameter and, using the `constructor` property, return `true` if the candidate is a `Dog`, otherwise return `false`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`joinDogFraternity` should be defined as a function.
|
||||
|
||||
```js
|
||||
assert(typeof joinDogFraternity === 'function');
|
||||
```
|
||||
|
||||
`joinDogFraternity` should return `true` if `candidate` is an instance of `Dog`.
|
||||
|
||||
```js
|
||||
assert(joinDogFraternity(new Dog('')) === true);
|
||||
```
|
||||
|
||||
`joinDogFraternity` should use the `constructor` property.
|
||||
|
||||
```js
|
||||
assert(/\.constructor/.test(__helpers.removeJSComments(code)) && !/instanceof/.test(__helpers.removeJSComments(code)));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
function joinDogFraternity(candidate) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
function joinDogFraternity(candidate) {
|
||||
return candidate.constructor === Dog;
|
||||
}
|
||||
```
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 587d7daf367417b2b2512b7f
|
||||
title: Change the Prototype to a New Object
|
||||
challengeType: 1
|
||||
forumTopicId: 301316
|
||||
dashedName: change-the-prototype-to-a-new-object
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Up until now you have been adding properties to the `prototype` individually:
|
||||
|
||||
```js
|
||||
Bird.prototype.numLegs = 2;
|
||||
```
|
||||
|
||||
This becomes tedious after more than a few properties.
|
||||
|
||||
```js
|
||||
Bird.prototype.eat = function() {
|
||||
console.log("nom nom nom");
|
||||
}
|
||||
|
||||
Bird.prototype.describe = function() {
|
||||
console.log("My name is " + this.name);
|
||||
}
|
||||
```
|
||||
|
||||
A more efficient way is to set the `prototype` to a new object that already contains the properties. This way, the properties are added all at once:
|
||||
|
||||
```js
|
||||
Bird.prototype = {
|
||||
numLegs: 2,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
},
|
||||
describe: function() {
|
||||
console.log("My name is " + this.name);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
Add the property `numLegs` and the two methods `eat()` and `describe()` to the `prototype` of `Dog` by setting the `prototype` to a new object.
|
||||
|
||||
# --hints--
|
||||
|
||||
`Dog.prototype` should be set to a new object.
|
||||
|
||||
```js
|
||||
assert(/Dog\.prototype\s*?=\s*?{/.test(__helpers.removeJSComments(code)));
|
||||
```
|
||||
|
||||
`Dog.prototype` should have the property `numLegs`.
|
||||
|
||||
```js
|
||||
assert(Dog.prototype.numLegs !== undefined);
|
||||
```
|
||||
|
||||
`Dog.prototype` should have the method `eat()`.
|
||||
|
||||
```js
|
||||
assert(typeof Dog.prototype.eat === 'function');
|
||||
```
|
||||
|
||||
`Dog.prototype` should have the method `describe()`.
|
||||
|
||||
```js
|
||||
assert(typeof Dog.prototype.describe === 'function');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
Dog.prototype = {
|
||||
// Only change code below this line
|
||||
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
Dog.prototype = {
|
||||
numLegs: 4,
|
||||
eat () {
|
||||
console.log('nom nom nom');
|
||||
},
|
||||
describe () {
|
||||
console.log('My name is ' + this.name);
|
||||
}
|
||||
};
|
||||
```
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 587d7daf367417b2b2512b80
|
||||
title: Remember to Set the Constructor Property when Changing the Prototype
|
||||
challengeType: 1
|
||||
forumTopicId: 301323
|
||||
dashedName: remember-to-set-the-constructor-property-when-changing-the-prototype
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
There is one crucial side effect of manually setting the prototype to a new object. It erases the `constructor` property! This property can be used to check which constructor function created the instance, but since the property has been overwritten, it now gives false results:
|
||||
|
||||
```js
|
||||
duck.constructor === Bird;
|
||||
duck.constructor === Object;
|
||||
duck instanceof Bird;
|
||||
```
|
||||
|
||||
In order, these expressions would evaluate to `false`, `true`, and `true`.
|
||||
|
||||
To fix this, whenever a prototype is manually set to a new object, remember to define the `constructor` property:
|
||||
|
||||
```js
|
||||
Bird.prototype = {
|
||||
constructor: Bird,
|
||||
numLegs: 2,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
},
|
||||
describe: function() {
|
||||
console.log("My name is " + this.name);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
Define the `constructor` property on the `Dog` `prototype`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`Dog.prototype` should set the `constructor` property.
|
||||
|
||||
```js
|
||||
assert(Dog.prototype.constructor === Dog);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
Dog.prototype = {
|
||||
|
||||
numLegs: 4,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
},
|
||||
describe: function() {
|
||||
console.log("My name is " + this.name);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
Dog.prototype = {
|
||||
constructor: Dog,
|
||||
numLegs: 4,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
},
|
||||
describe: function() {
|
||||
console.log("My name is " + this.name);
|
||||
}
|
||||
};
|
||||
```
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
---
|
||||
id: 587d7db0367417b2b2512b81
|
||||
title: Understand Where an Object’s Prototype Comes From
|
||||
challengeType: 1
|
||||
forumTopicId: 301330
|
||||
dashedName: understand-where-an-objects-prototype-comes-from
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Just like people inherit genes from their parents, an object inherits its `prototype` directly from the constructor function that created it. For example, here the `Bird` constructor creates the `duck` object:
|
||||
|
||||
```js
|
||||
function Bird(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
let duck = new Bird("Donald");
|
||||
```
|
||||
|
||||
`duck` inherits its `prototype` from the `Bird` constructor function. You can show this relationship with the `isPrototypeOf` method:
|
||||
|
||||
```js
|
||||
Bird.prototype.isPrototypeOf(duck);
|
||||
```
|
||||
|
||||
This would return `true`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use `isPrototypeOf` to check the `prototype` of `beagle`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should show that `Dog.prototype` is the `prototype` of `beagle`
|
||||
|
||||
```js
|
||||
assert(/Dog\.prototype\.isPrototypeOf\(beagle\)/.test(__helpers.removeJSComments(code)));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
let beagle = new Dog("Snoopy");
|
||||
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
let beagle = new Dog("Snoopy");
|
||||
Dog.prototype.isPrototypeOf(beagle);
|
||||
```
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 587d7db0367417b2b2512b82
|
||||
title: Understand the Prototype Chain
|
||||
challengeType: 1
|
||||
forumTopicId: 301329
|
||||
dashedName: understand-the-prototype-chain
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
All objects in JavaScript (with a few exceptions) have a `prototype`. Also, an object’s `prototype` itself is an object.
|
||||
|
||||
```js
|
||||
function Bird(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
typeof Bird.prototype;
|
||||
```
|
||||
|
||||
Because a `prototype` is an object, a `prototype` can have its own `prototype`! In this case, the `prototype` of `Bird.prototype` is `Object.prototype`:
|
||||
|
||||
```js
|
||||
Object.prototype.isPrototypeOf(Bird.prototype);
|
||||
```
|
||||
|
||||
How is this useful? You may recall the `hasOwnProperty` method from a previous challenge:
|
||||
|
||||
```js
|
||||
let duck = new Bird("Donald");
|
||||
duck.hasOwnProperty("name");
|
||||
```
|
||||
|
||||
The `hasOwnProperty` method is defined in `Object.prototype`, which can be accessed by `Bird.prototype`, which can then be accessed by `duck`. This is an example of the `prototype` chain. In this `prototype` chain, `Bird` is the `supertype` for `duck`, while `duck` is the `subtype`. `Object` is a `supertype` for both `Bird` and `duck`. `Object` is a `supertype` for all objects in JavaScript. Therefore, any object can use the `hasOwnProperty` method.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Modify the code to show the correct prototype chain.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your code should show that `Object.prototype` is the prototype of `Dog.prototype`
|
||||
|
||||
```js
|
||||
assert(/Object\.prototype\.isPrototypeOf/.test(__helpers.removeJSComments(code)));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
let beagle = new Dog("Snoopy");
|
||||
|
||||
Dog.prototype.isPrototypeOf(beagle); // yields true
|
||||
|
||||
// Fix the code below so that it evaluates to true
|
||||
???.isPrototypeOf(Dog.prototype);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Dog(name) {
|
||||
this.name = name;
|
||||
}
|
||||
let beagle = new Dog("Snoopy");
|
||||
Dog.prototype.isPrototypeOf(beagle);
|
||||
Object.prototype.isPrototypeOf(Dog.prototype);
|
||||
```
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
---
|
||||
id: 587d7db0367417b2b2512b83
|
||||
title: Use Inheritance So You Don't Repeat Yourself
|
||||
challengeType: 1
|
||||
forumTopicId: 301334
|
||||
dashedName: use-inheritance-so-you-dont-repeat-yourself
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
There's a principle in programming called <dfn>Don't Repeat Yourself (DRY)</dfn>. The reason repeated code is a problem is because any change requires fixing code in multiple places. This usually means more work for programmers and more room for errors.
|
||||
|
||||
Notice in the example below that the `describe` method is shared by `Bird` and `Dog`:
|
||||
|
||||
```js
|
||||
Bird.prototype = {
|
||||
constructor: Bird,
|
||||
describe: function() {
|
||||
console.log("My name is " + this.name);
|
||||
}
|
||||
};
|
||||
|
||||
Dog.prototype = {
|
||||
constructor: Dog,
|
||||
describe: function() {
|
||||
console.log("My name is " + this.name);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
The `describe` method is repeated in two places. The code can be edited to follow the DRY principle by creating a `supertype` (or parent) called `Animal`:
|
||||
|
||||
```js
|
||||
function Animal() { };
|
||||
|
||||
Animal.prototype = {
|
||||
constructor: Animal,
|
||||
describe: function() {
|
||||
console.log("My name is " + this.name);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
Since `Animal` includes the `describe` method, you can remove it from `Bird` and `Dog`:
|
||||
|
||||
```js
|
||||
Bird.prototype = {
|
||||
constructor: Bird
|
||||
};
|
||||
|
||||
Dog.prototype = {
|
||||
constructor: Dog
|
||||
};
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
The `eat` method is repeated in both `Cat` and `Bear`. Edit the code in the spirit of DRY by moving the `eat` method to the `Animal` `supertype`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`Animal.prototype` should have the `eat` property.
|
||||
|
||||
```js
|
||||
assert(Animal.prototype.hasOwnProperty('eat'));
|
||||
```
|
||||
|
||||
`Bear.prototype` should not have the `eat` property.
|
||||
|
||||
```js
|
||||
assert(!Bear.prototype.hasOwnProperty('eat'));
|
||||
```
|
||||
|
||||
`Cat.prototype` should not have the `eat` property.
|
||||
|
||||
```js
|
||||
assert(!Cat.prototype.hasOwnProperty('eat'));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Cat(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
Cat.prototype = {
|
||||
constructor: Cat,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
}
|
||||
};
|
||||
|
||||
function Bear(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
Bear.prototype = {
|
||||
constructor: Bear,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
}
|
||||
};
|
||||
|
||||
function Animal() { }
|
||||
|
||||
Animal.prototype = {
|
||||
constructor: Animal,
|
||||
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Cat(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
Cat.prototype = {
|
||||
constructor: Cat
|
||||
};
|
||||
|
||||
function Bear(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
Bear.prototype = {
|
||||
constructor: Bear
|
||||
};
|
||||
|
||||
function Animal() { }
|
||||
|
||||
Animal.prototype = {
|
||||
constructor: Animal,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
}
|
||||
};
|
||||
```
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
---
|
||||
id: 587d7db0367417b2b2512b84
|
||||
title: Inherit Behaviors from a Supertype
|
||||
challengeType: 1
|
||||
forumTopicId: 301319
|
||||
dashedName: inherit-behaviors-from-a-supertype
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the previous challenge, you created a `supertype` called `Animal` that defined behaviors shared by all animals:
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
Animal.prototype.eat = function() {
|
||||
console.log("nom nom nom");
|
||||
};
|
||||
```
|
||||
|
||||
This and the next challenge will cover how to reuse the methods of `Animal` inside `Bird` and `Dog` without defining them again. It uses a technique called inheritance. This challenge covers the first step: make an instance of the `supertype` (or parent). You already know one way to create an instance of `Animal` using the `new` operator:
|
||||
|
||||
```js
|
||||
let animal = new Animal();
|
||||
```
|
||||
|
||||
There are some disadvantages when using this syntax for inheritance, which are too complex for the scope of this challenge. Instead, here's an alternative approach without those disadvantages:
|
||||
|
||||
```js
|
||||
let animal = Object.create(Animal.prototype);
|
||||
```
|
||||
|
||||
`Object.create(obj)` creates a new object, and sets `obj` as the new object's `prototype`. Recall that the `prototype` is like the "recipe" for creating an object. By setting the `prototype` of `animal` to be the `prototype` of `Animal`, you are effectively giving the `animal` instance the same "recipe" as any other instance of `Animal`.
|
||||
|
||||
```js
|
||||
animal.eat();
|
||||
animal instanceof Animal;
|
||||
```
|
||||
|
||||
The `instanceof` method here would return `true`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use `Object.create` to make two instances of `Animal` named `duck` and `beagle`.
|
||||
|
||||
# --hints--
|
||||
|
||||
The `duck` variable should be defined.
|
||||
|
||||
```js
|
||||
assert(typeof duck !== 'undefined');
|
||||
```
|
||||
|
||||
The `beagle` variable should be defined.
|
||||
|
||||
```js
|
||||
assert(typeof beagle !== 'undefined');
|
||||
```
|
||||
|
||||
The `duck` variable should be initialised with `Object.create`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
/(let|const|var)\s{1,}duck\s*=\s*Object\.create\s*\(\s*Animal\.prototype\s*\)\s*/.test(
|
||||
__helpers.removeJSComments(code)
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
The `beagle` variable should be initialised with `Object.create`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
/(let|const|var)\s{1,}beagle\s*=\s*Object\.create\s*\(\s*Animal\.prototype\s*\)\s*/.test(
|
||||
__helpers.removeJSComments(code)
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`duck` should have a `prototype` of `Animal`.
|
||||
|
||||
```js
|
||||
assert(duck instanceof Animal);
|
||||
```
|
||||
|
||||
`beagle` should have a `prototype` of `Animal`.
|
||||
|
||||
```js
|
||||
assert(beagle instanceof Animal);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
|
||||
Animal.prototype = {
|
||||
constructor: Animal,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
}
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
let duck; // Change this line
|
||||
let beagle; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
|
||||
Animal.prototype = {
|
||||
constructor: Animal,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
}
|
||||
};
|
||||
let duck = Object.create(Animal.prototype);
|
||||
let beagle = Object.create(Animal.prototype);
|
||||
|
||||
duck.eat();
|
||||
beagle.eat();
|
||||
```
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 587d7db1367417b2b2512b85
|
||||
title: Set the Child's Prototype to an Instance of the Parent
|
||||
challengeType: 1
|
||||
forumTopicId: 301325
|
||||
dashedName: set-the-childs-prototype-to-an-instance-of-the-parent
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the previous challenge you saw the first step for inheriting behavior from the supertype (or parent) `Animal`: making a new instance of `Animal`.
|
||||
|
||||
This challenge covers the next step: set the `prototype` of the subtype (or child)—in this case, `Bird`—to be an instance of `Animal`.
|
||||
|
||||
```js
|
||||
Bird.prototype = Object.create(Animal.prototype);
|
||||
```
|
||||
|
||||
Remember that the `prototype` is like the "recipe" for creating an object. In a way, the recipe for `Bird` now includes all the key "ingredients" from `Animal`.
|
||||
|
||||
```js
|
||||
let duck = new Bird("Donald");
|
||||
duck.eat();
|
||||
```
|
||||
|
||||
`duck` inherits all of `Animal`'s properties, including the `eat` method.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Modify the code so that instances of `Dog` inherit from `Animal`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`Dog.prototype` should be an instance of `Animal`.
|
||||
|
||||
```js
|
||||
assert(Animal.prototype.isPrototypeOf(Dog.prototype));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
|
||||
Animal.prototype = {
|
||||
constructor: Animal,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
}
|
||||
};
|
||||
|
||||
function Dog() { }
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
let beagle = new Dog();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
|
||||
Animal.prototype = {
|
||||
constructor: Animal,
|
||||
eat: function() {
|
||||
console.log("nom nom nom");
|
||||
}
|
||||
};
|
||||
|
||||
function Dog() { }
|
||||
Dog.prototype = Object.create(Animal.prototype);
|
||||
|
||||
let beagle = new Dog();
|
||||
beagle.eat();
|
||||
```
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 587d7db1367417b2b2512b86
|
||||
title: Reset an Inherited Constructor Property
|
||||
challengeType: 1
|
||||
forumTopicId: 301324
|
||||
dashedName: reset-an-inherited-constructor-property
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
When an object inherits its `prototype` from another object, it also inherits the supertype's constructor property.
|
||||
|
||||
Here's an example:
|
||||
|
||||
```js
|
||||
function Bird() { }
|
||||
Bird.prototype = Object.create(Animal.prototype);
|
||||
let duck = new Bird();
|
||||
duck.constructor
|
||||
```
|
||||
|
||||
But `duck` and all instances of `Bird` should show that they were constructed by `Bird` and not `Animal`. To do so, you can manually set the constructor property of `Bird` to the `Bird` object:
|
||||
|
||||
```js
|
||||
Bird.prototype.constructor = Bird;
|
||||
duck.constructor
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
Fix the code so `duck.constructor` and `beagle.constructor` return their respective constructors.
|
||||
|
||||
# --hints--
|
||||
|
||||
`Bird.prototype` should be an instance of `Animal`.
|
||||
|
||||
```js
|
||||
assert(Animal.prototype.isPrototypeOf(Bird.prototype));
|
||||
```
|
||||
|
||||
`duck.constructor` should return `Bird`.
|
||||
|
||||
```js
|
||||
assert(duck.constructor === Bird);
|
||||
```
|
||||
|
||||
`Dog.prototype` should be an instance of `Animal`.
|
||||
|
||||
```js
|
||||
assert(Animal.prototype.isPrototypeOf(Dog.prototype));
|
||||
```
|
||||
|
||||
`beagle.constructor` should return `Dog`.
|
||||
|
||||
```js
|
||||
assert(beagle.constructor === Dog);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
function Bird() { }
|
||||
function Dog() { }
|
||||
|
||||
Bird.prototype = Object.create(Animal.prototype);
|
||||
Dog.prototype = Object.create(Animal.prototype);
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
let duck = new Bird();
|
||||
let beagle = new Dog();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
function Bird() { }
|
||||
function Dog() { }
|
||||
Bird.prototype = Object.create(Animal.prototype);
|
||||
Dog.prototype = Object.create(Animal.prototype);
|
||||
Dog.prototype.constructor = Dog;
|
||||
Bird.prototype.constructor = Bird;
|
||||
let duck = new Bird();
|
||||
let beagle = new Dog();
|
||||
```
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
---
|
||||
id: 587d7db1367417b2b2512b87
|
||||
title: Add Methods After Inheritance
|
||||
challengeType: 1
|
||||
forumTopicId: 301315
|
||||
dashedName: add-methods-after-inheritance
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A constructor function that inherits its `prototype` object from a supertype constructor function can still have its own methods in addition to inherited methods.
|
||||
|
||||
For example, `Bird` is a constructor that inherits its `prototype` from `Animal`:
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
Animal.prototype.eat = function() {
|
||||
console.log("nom nom nom");
|
||||
};
|
||||
function Bird() { }
|
||||
Bird.prototype = Object.create(Animal.prototype);
|
||||
Bird.prototype.constructor = Bird;
|
||||
```
|
||||
|
||||
In addition to what is inherited from `Animal`, you want to add behavior that is unique to `Bird` objects. Here, `Bird` will get a `fly()` function. Functions are added to `Bird's` `prototype` the same way as any constructor function:
|
||||
|
||||
```js
|
||||
Bird.prototype.fly = function() {
|
||||
console.log("I'm flying!");
|
||||
};
|
||||
```
|
||||
|
||||
Now instances of `Bird` will have both `eat()` and `fly()` methods:
|
||||
|
||||
```js
|
||||
let duck = new Bird();
|
||||
duck.eat();
|
||||
duck.fly();
|
||||
```
|
||||
|
||||
`duck.eat()` would display the string `nom nom nom` in the console, and `duck.fly()` would display the string `I'm flying!`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Add all necessary code so the `Dog` object inherits from `Animal` and the `Dog`'s `prototype` constructor is set to `Dog`. Then add a `bark()` method to the `Dog` object so that `beagle` can both `eat()` and `bark()`. The `bark()` method should print `Woof!` to the console.
|
||||
|
||||
# --hints--
|
||||
|
||||
`Animal` should not respond to the `bark()` method.
|
||||
|
||||
```js
|
||||
assert(typeof Animal.prototype.bark == 'undefined');
|
||||
```
|
||||
|
||||
`Dog` should inherit the `eat()` method from `Animal`.
|
||||
|
||||
```js
|
||||
assert(typeof Dog.prototype.eat == 'function');
|
||||
```
|
||||
|
||||
The `Dog` prototype should have a `bark()` method.
|
||||
|
||||
```js
|
||||
assert('bark' in Dog.prototype);
|
||||
```
|
||||
|
||||
`beagle` should be an `instanceof` `Animal`.
|
||||
|
||||
```js
|
||||
assert(beagle instanceof Animal);
|
||||
```
|
||||
|
||||
The constructor for `beagle` should be set to `Dog`.
|
||||
|
||||
```js
|
||||
assert(beagle.constructor === Dog);
|
||||
```
|
||||
|
||||
`beagle.eat()` should log the string `nom nom nom`
|
||||
|
||||
```js
|
||||
const spy = __helpers.spyOn(console, 'log');
|
||||
beagle.eat();
|
||||
assert.lengthOf(spy.calls,1);
|
||||
assert(spy.calls[0][0] == 'nom nom nom');
|
||||
```
|
||||
|
||||
`beagle.bark()` should log the string `Woof!`
|
||||
|
||||
```js
|
||||
const spy = __helpers.spyOn(console, 'log');
|
||||
beagle.bark();
|
||||
assert.lengthOf(spy.calls,1);
|
||||
assert(spy.calls[0][0] == 'Woof!');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
Animal.prototype.eat = function() { console.log("nom nom nom"); };
|
||||
|
||||
function Dog() { }
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
let beagle = new Dog();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
Animal.prototype.eat = function() { console.log("nom nom nom"); };
|
||||
|
||||
function Dog() { }
|
||||
Dog.prototype = Object.create(Animal.prototype);
|
||||
Dog.prototype.constructor = Dog;
|
||||
Dog.prototype.bark = function () {
|
||||
console.log('Woof!');
|
||||
};
|
||||
let beagle = new Dog();
|
||||
|
||||
beagle.eat();
|
||||
beagle.bark();
|
||||
```
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
---
|
||||
id: 587d7db1367417b2b2512b88
|
||||
title: Override Inherited Methods
|
||||
challengeType: 1
|
||||
forumTopicId: 301322
|
||||
dashedName: override-inherited-methods
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In previous lessons, you learned that an object can inherit its behavior (methods) from another object by referencing its `prototype` object:
|
||||
|
||||
```js
|
||||
ChildObject.prototype = Object.create(ParentObject.prototype);
|
||||
```
|
||||
|
||||
Then the `ChildObject` received its own methods by chaining them onto its `prototype`:
|
||||
|
||||
```js
|
||||
ChildObject.prototype.methodName = function() {...};
|
||||
```
|
||||
|
||||
It's possible to override an inherited method. It's done the same way - by adding a method to `ChildObject.prototype` using the same method name as the one to override. Here's an example of `Bird` overriding the `eat()` method inherited from `Animal`:
|
||||
|
||||
```js
|
||||
function Animal() { }
|
||||
Animal.prototype.eat = function() {
|
||||
return "nom nom nom";
|
||||
};
|
||||
function Bird() { }
|
||||
|
||||
Bird.prototype = Object.create(Animal.prototype);
|
||||
|
||||
Bird.prototype.eat = function() {
|
||||
return "peck peck peck";
|
||||
};
|
||||
```
|
||||
|
||||
If you have an instance `let duck = new Bird();` and you call `duck.eat()`, this is how JavaScript looks for the method on the `prototype` chain of `duck`:
|
||||
|
||||
1. `duck` => Is `eat()` defined here? No.
|
||||
2. `Bird` => Is `eat()` defined here? => Yes. Execute it and stop searching.
|
||||
3. `Animal` => `eat()` is also defined, but JavaScript stopped searching before reaching this level.
|
||||
4. Object => JavaScript stopped searching before reaching this level.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Override the `fly()` method for `Penguin` so that it returns the string `Alas, this is a flightless bird.`
|
||||
|
||||
# --hints--
|
||||
|
||||
`penguin.fly()` should return the string `Alas, this is a flightless bird.`
|
||||
|
||||
```js
|
||||
assert(penguin.fly() === 'Alas, this is a flightless bird.');
|
||||
```
|
||||
|
||||
The `bird.fly()` method should return the string `I am flying!`
|
||||
|
||||
```js
|
||||
assert(new Bird().fly() === 'I am flying!');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Bird() { }
|
||||
|
||||
Bird.prototype.fly = function() { return "I am flying!"; };
|
||||
|
||||
function Penguin() { }
|
||||
Penguin.prototype = Object.create(Bird.prototype);
|
||||
Penguin.prototype.constructor = Penguin;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
let penguin = new Penguin();
|
||||
console.log(penguin.fly());
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Bird() { }
|
||||
|
||||
Bird.prototype.fly = function() { return "I am flying!"; };
|
||||
|
||||
function Penguin() { }
|
||||
Penguin.prototype = Object.create(Bird.prototype);
|
||||
Penguin.prototype.constructor = Penguin;
|
||||
Penguin.prototype.fly = () => 'Alas, this is a flightless bird.';
|
||||
let penguin = new Penguin();
|
||||
console.log(penguin.fly());
|
||||
```
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
---
|
||||
id: 587d7db2367417b2b2512b89
|
||||
title: Use a Mixin to Add Common Behavior Between Unrelated Objects
|
||||
challengeType: 1
|
||||
forumTopicId: 301331
|
||||
dashedName: use-a-mixin-to-add-common-behavior-between-unrelated-objects
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
As you have seen, behavior is shared through inheritance. However, there are cases when inheritance is not the best solution. Inheritance does not work well for unrelated objects like `Bird` and `Airplane`. They can both fly, but a `Bird` is not a type of `Airplane` and vice versa.
|
||||
|
||||
For unrelated objects, it's better to use <dfn>mixins</dfn>. A mixin allows other objects to use a collection of functions.
|
||||
|
||||
```js
|
||||
let flyMixin = function(obj) {
|
||||
obj.fly = function() {
|
||||
console.log("Flying, wooosh!");
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
The `flyMixin` takes any object and gives it the `fly` method.
|
||||
|
||||
```js
|
||||
let bird = {
|
||||
name: "Donald",
|
||||
numLegs: 2
|
||||
};
|
||||
|
||||
let plane = {
|
||||
model: "777",
|
||||
numPassengers: 524
|
||||
};
|
||||
|
||||
flyMixin(bird);
|
||||
flyMixin(plane);
|
||||
```
|
||||
|
||||
Here `bird` and `plane` are passed into `flyMixin`, which then assigns the `fly` function to each object. Now `bird` and `plane` can both fly:
|
||||
|
||||
```js
|
||||
bird.fly();
|
||||
plane.fly();
|
||||
```
|
||||
|
||||
The console would display the string `Flying, wooosh!` twice, once for each `.fly()` call.
|
||||
|
||||
Note how the mixin allows for the same `fly` method to be reused by unrelated objects `bird` and `plane`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create a mixin named `glideMixin` that defines a method named `glide`. Then use the `glideMixin` to give both `bird` and `boat` the ability to glide.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your code should declare a `glideMixin` variable that is a function.
|
||||
|
||||
```js
|
||||
assert(typeof glideMixin === 'function');
|
||||
```
|
||||
|
||||
Your code should use the `glideMixin` on the `bird` object to give it the `glide` method.
|
||||
|
||||
```js
|
||||
assert(typeof bird.glide === 'function');
|
||||
```
|
||||
|
||||
Your code should use the `glideMixin` on the `boat` object to give it the `glide` method.
|
||||
|
||||
```js
|
||||
assert(typeof boat.glide === 'function');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let bird = {
|
||||
name: "Donald",
|
||||
numLegs: 2
|
||||
};
|
||||
|
||||
let boat = {
|
||||
name: "Warrior",
|
||||
type: "race-boat"
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let bird = {
|
||||
name: "Donald",
|
||||
numLegs: 2
|
||||
};
|
||||
|
||||
let boat = {
|
||||
name: "Warrior",
|
||||
type: "race-boat"
|
||||
};
|
||||
function glideMixin (obj) {
|
||||
obj.glide = () => 'Gliding!';
|
||||
}
|
||||
|
||||
glideMixin(bird);
|
||||
glideMixin(boat);
|
||||
```
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 587d7db2367417b2b2512b8a
|
||||
title: >-
|
||||
Use Closure to Protect Properties Within an Object from Being Modified
|
||||
Externally
|
||||
challengeType: 1
|
||||
forumTopicId: 18234
|
||||
dashedName: >-
|
||||
use-closure-to-protect-properties-within-an-object-from-being-modified-externally
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the previous challenge, `bird` had a public property `name`. It is considered public because it can be accessed and changed outside of `bird`'s definition.
|
||||
|
||||
```js
|
||||
bird.name = "Duffy";
|
||||
```
|
||||
|
||||
Therefore, any part of your code can easily change the name of `bird` to any value. Think about things like passwords and bank accounts being easily changeable by any part of your codebase. That could cause a lot of issues.
|
||||
|
||||
The simplest way to make this public property private is by creating a variable within the constructor function. This changes the scope of that variable to be within the constructor function versus available globally. This way, the variable can only be accessed and changed by methods also within the constructor function.
|
||||
|
||||
```js
|
||||
function Bird() {
|
||||
let hatchedEgg = 10;
|
||||
|
||||
this.getHatchedEggCount = function() {
|
||||
return hatchedEgg;
|
||||
};
|
||||
}
|
||||
let ducky = new Bird();
|
||||
ducky.getHatchedEggCount();
|
||||
```
|
||||
|
||||
Here `getHatchedEggCount` is a privileged method, because it has access to the private variable `hatchedEgg`. This is possible because `hatchedEgg` is declared in the same context as `getHatchedEggCount`. In JavaScript, a function always has access to the context in which it was created. This is called `closure`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Change how `weight` is declared in the `Bird` function so it is a private variable. Then, create a method `getWeight` that returns the value of `weight` 15.
|
||||
|
||||
# --hints--
|
||||
|
||||
The `weight` property should be a private variable and should be assigned the value of `15`.
|
||||
|
||||
```js
|
||||
assert(__helpers.removeJSComments(code).match(/(var|let|const)\s+weight\s*\=\s*15\;?/g));
|
||||
```
|
||||
|
||||
Your code should create a method in `Bird` called `getWeight` that returns the value of the private variable `weight`.
|
||||
|
||||
```js
|
||||
assert(new Bird().getWeight() === 15);
|
||||
```
|
||||
|
||||
Your `getWeight` function should return the private variable `weight`.
|
||||
|
||||
```js
|
||||
assert(__helpers.removeJSComments(code).match(/((return\s+)|(\(\s*\)\s*\=\>\s*))weight\;?/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Bird() {
|
||||
this.weight = 15;
|
||||
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Bird() {
|
||||
let weight = 15;
|
||||
|
||||
this.getWeight = () => weight;
|
||||
}
|
||||
```
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 587d7db2367417b2b2512b8b
|
||||
title: Understand the Immediately Invoked Function Expression (IIFE)
|
||||
challengeType: 1
|
||||
forumTopicId: 301328
|
||||
dashedName: understand-the-immediately-invoked-function-expression-iife
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A common pattern in JavaScript is to execute a function as soon as it is declared:
|
||||
|
||||
```js
|
||||
(function () {
|
||||
console.log("Chirp, chirp!");
|
||||
})();
|
||||
```
|
||||
|
||||
This is an anonymous function expression that executes right away, and outputs `Chirp, chirp!` immediately.
|
||||
|
||||
Note that the function has no name and is not stored in a variable. The two parentheses () at the end of the function expression cause it to be immediately executed or invoked. This pattern is known as an <dfn>immediately invoked function expression</dfn> or <dfn>IIFE</dfn>.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Rewrite the function `makeNest` and remove its call so instead it's an anonymous immediately invoked function expression (IIFE).
|
||||
|
||||
# --hints--
|
||||
|
||||
The function should be anonymous.
|
||||
|
||||
```js
|
||||
assert(/\((function|\(\))(=>|\(\)){?/.test(__helpers.removeJSComments(code).replace(/\s/g, '')));
|
||||
```
|
||||
|
||||
Your function should have parentheses at the end of the expression to call it immediately.
|
||||
|
||||
```js
|
||||
assert(/\(.*(\)\(|\}\(\))\)/.test(__helpers.removeJSComments(code).replace(/[\s;]/g, '')));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function makeNest() {
|
||||
console.log("A cozy nest is ready");
|
||||
}
|
||||
|
||||
makeNest();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
(function () {
|
||||
console.log("A cozy nest is ready");
|
||||
})();
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```js
|
||||
(function () {
|
||||
console.log("A cozy nest is ready");
|
||||
}());
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```js
|
||||
(() => {
|
||||
console.log("A cozy nest is ready");
|
||||
})();
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```js
|
||||
(() =>
|
||||
console.log("A cozy nest is ready")
|
||||
)();
|
||||
```
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
---
|
||||
id: 587d7db2367417b2b2512b8c
|
||||
title: Use an IIFE to Create a Module
|
||||
challengeType: 1
|
||||
forumTopicId: 301332
|
||||
dashedName: use-an-iife-to-create-a-module
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
An immediately invoked function expression (IIFE) is often used to group related functionality into a single object or <dfn>module</dfn>. For example, an earlier challenge defined two mixins:
|
||||
|
||||
```js
|
||||
function glideMixin(obj) {
|
||||
obj.glide = function() {
|
||||
console.log("Gliding on the water");
|
||||
};
|
||||
}
|
||||
function flyMixin(obj) {
|
||||
obj.fly = function() {
|
||||
console.log("Flying, wooosh!");
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
We can group these mixins into a module as follows:
|
||||
|
||||
```js
|
||||
let motionModule = (function () {
|
||||
return {
|
||||
glideMixin: function(obj) {
|
||||
obj.glide = function() {
|
||||
console.log("Gliding on the water");
|
||||
};
|
||||
},
|
||||
flyMixin: function(obj) {
|
||||
obj.fly = function() {
|
||||
console.log("Flying, wooosh!");
|
||||
};
|
||||
}
|
||||
}
|
||||
})();
|
||||
```
|
||||
|
||||
Note that you have an immediately invoked function expression (IIFE) that returns an object `motionModule`. This returned object contains all of the mixin behaviors as properties of the object. The advantage of the module pattern is that all of the motion behaviors can be packaged into a single object that can then be used by other parts of your code. Here is an example using it:
|
||||
|
||||
```js
|
||||
motionModule.glideMixin(duck);
|
||||
duck.glide();
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create a module named `funModule` to wrap the two mixins `isCuteMixin` and `singMixin`. `funModule` should return an object.
|
||||
|
||||
# --hints--
|
||||
|
||||
`funModule` should be defined and return an object.
|
||||
|
||||
```js
|
||||
assert(typeof funModule === 'object');
|
||||
```
|
||||
|
||||
`funModule.isCuteMixin` should access a function.
|
||||
|
||||
```js
|
||||
assert(typeof funModule.isCuteMixin === 'function');
|
||||
```
|
||||
|
||||
`funModule.singMixin` should access a function.
|
||||
|
||||
```js
|
||||
assert(typeof funModule.singMixin === 'function');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let isCuteMixin = function(obj) {
|
||||
obj.isCute = function() {
|
||||
return true;
|
||||
};
|
||||
};
|
||||
let singMixin = function(obj) {
|
||||
obj.sing = function() {
|
||||
console.log("Singing to an awesome tune");
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const funModule = (function () {
|
||||
return {
|
||||
isCuteMixin: obj => {
|
||||
obj.isCute = () => true;
|
||||
},
|
||||
singMixin: obj => {
|
||||
obj.sing = () => console.log("Singing to an awesome tune");
|
||||
}
|
||||
};
|
||||
})();
|
||||
```
|
||||
Reference in New Issue
Block a user