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-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
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
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/use-a-mixin-to-add-common-behavior-between-unrelated-objects.english.md

94 lines
3.2 KiB
Markdown
Raw Normal View History

---
id: 587d7db2367417b2b2512b89
title: Use a Mixin to Add Common Behavior Between Unrelated Objects
challengeType: 1
---
## Description
<section id='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 <code>Bird</code> and <code>Airplane</code>. They can both fly, but a <code>Bird</code> is not a type of <code>Airplane</code> and vice versa.
For unrelated objects, it's better to use <code>mixins</code>. A <code>mixin</code> allows other objects to use a collection of functions.
<blockquote>let flyMixin = function(obj) {<br>&nbsp;&nbsp;obj.fly = function() {<br>&nbsp;&nbsp;&nbsp;&nbsp;console.log("Flying, wooosh!");<br>&nbsp;&nbsp;}<br>};</blockquote>
The <code>flyMixin</code> takes any object and gives it the <code>fly</code> method.
<blockquote>let bird = {<br>&nbsp;&nbsp;name: "Donald",<br>&nbsp;&nbsp;numLegs: 2<br>};<br><br>let plane = {<br>&nbsp;&nbsp;model: "777",<br>&nbsp;&nbsp;numPassengers: 524<br>};<br><br>flyMixin(bird);<br>flyMixin(plane);</blockquote>
Here <code>bird</code> and <code>plane</code> are passed into <code>flyMixin</code>, which then assigns the <code>fly</code> function to each object. Now <code>bird</code> and <code>plane</code> can both fly:
<blockquote>bird.fly(); // prints "Flying, wooosh!"<br>plane.fly(); // prints "Flying, wooosh!"</blockquote>
Note how the <code>mixin</code> allows for the same <code>fly</code> method to be reused by unrelated objects <code>bird</code> and <code>plane</code>.
</section>
## Instructions
<section id='instructions'>
Create a <code>mixin</code> named <code>glideMixin</code> that defines a method named <code>glide</code>. Then use the <code>glideMixin</code> to give both <code>bird</code> and <code>boat</code> the ability to glide.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: Your code should declare a <code>glideMixin</code> variable that is a function.
testString: assert(typeof glideMixin === "function", 'Your code should declare a <code>glideMixin</code> variable that is a function.');
- text: Your code should use the <code>glideMixin</code> on the <code>bird</code> object to give it the <code>glide</code> method.
testString: assert(typeof bird.glide === "function", 'Your code should use the <code>glideMixin</code> on the <code>bird</code> object to give it the <code>glide</code> method.');
- text: Your code should use the <code>glideMixin</code> on the <code>boat</code> object to give it the <code>glide</code> method.
testString: assert(typeof boat.glide === "function", 'Your code should use the <code>glideMixin</code> on the <code>boat</code> object to give it the <code>glide</code> method.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
let bird = {
name: "Donald",
numLegs: 2
};
let boat = {
name: "Warrior",
type: "race-boat"
};
// Add your code below this line
```
</div>
</section>
## Solution
<section id='solution'>
```js
let bird = {
name: "Donald",
numLegs: 2
};
let boat = {
name: "Warrior",
type: "race-boat"
};
function glideMixin (obj) {
obj.glide = () => 'Gliding!';
}
glideMixin(bird);
glideMixin(boat);
```
</section>