.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3d
accessibility
agile
algorithms
android-development
angular
apache
aspnet
bash
blender
blockchain
bootstrap
bsd-os
bulma
c
canvas
certifications
apis-and-microservices
coding-interview-prep
data-visualization
front-end-libraries
information-security-and-quality-assurance
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
index.md
change-the-prototype-to-a-new-object
create-a-basic-javascript-object
create-a-method-on-an-object
define-a-constructor-function
extend-constructors-to-receive-arguments
inherit-behaviors-from-a-supertype
iterate-over-all-properties
make-code-more-reusable-with-the-this-keyword
override-inherited-methods
remember-to-set-the-constructor-property-when-changing-the-prototype
reset-an-inherited-constructor-property
set-the-childs-prototype-to-an-instance-of-the-parent
understand-own-properties
understand-the-constructor-property
understand-the-immediately-invoked-function-expression-iife
understand-the-prototype-chain
understand-where-an-objects-prototype-comes-from
use-a-constructor-to-create-objects
use-a-mixin-to-add-common-behavior-between-unrelated-objects
use-an-iife-to-create-a-module
use-closure-to-protect-properties-within-an-object-from-being-modified-externally
use-dot-notation-to-access-the-properties-of-an-object
use-inheritance-so-you-dont-repeat-yourself
use-prototype-properties-to-reduce-duplicate-code
verify-an-objects-constructor-with-instanceof
index.md
regular-expressions
index.md
responsive-web-design
index.md
chef
clojure
cloud-development
codeigniter
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
dns
docker
documentation
drupal
electron
elixir
elm
erlang
fsharp
game-development
gatsbyjs
git
go
graphql
groovy
haskell
haxe
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mariadb
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
optical-alignment
php
powershell
product-design
progressive-web-apps
puppet
python
r
raspberry-pi
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
sass
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tomcat
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
portuguese
russian
spanish
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.french.md
README.italian.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
43 lines
879 B
Markdown
43 lines
879 B
Markdown
![]() |
---
|
||
|
title: Add Methods After Inheritance
|
||
|
---
|
||
|
## Add Methods After Inheritance
|
||
|
|
||
|
### Method
|
||
|
|
||
|
Just like the following example, a new instance of an object - `Dog` - must be created and the `prototype` must be set.
|
||
|
|
||
|
```javascript
|
||
|
|
||
|
function Bird() { }
|
||
|
Bird.prototype = Object.create(Animal.prototype);
|
||
|
Bird.prototype.constructor = Bird;
|
||
|
|
||
|
```
|
||
|
|
||
|
Then a new function - `bark()` - must be added to the Dog prototype.
|
||
|
|
||
|
### Solution
|
||
|
|
||
|
```javascript
|
||
|
|
||
|
function Animal() { }
|
||
|
Animal.prototype.eat = function() { console.log("nom nom nom"); };
|
||
|
|
||
|
function Dog() { }
|
||
|
|
||
|
// Add your code below this line
|
||
|
Dog.prototype = Object.create(Animal.prototype);
|
||
|
Dog.prototype.constructor = Dog;
|
||
|
Dog.prototype.bark = function() {
|
||
|
console.log("Woof woof!");
|
||
|
};
|
||
|
// Add your code above this line
|
||
|
|
||
|
let beagle = new Dog();
|
||
|
|
||
|
beagle.eat(); // Should print "nom nom nom"
|
||
|
beagle.bark(); // Should print "Woof!"
|
||
|
|
||
|
```
|