Forked from ericelliott/class-inheritance-example.js
Created
January 9, 2019 12:19
-
-
Save Elemino/095d3150df2e2f77b12c61664b4436b4 to your computer and use it in GitHub Desktop.
Class Inheritance Example http://codepen.io/ericelliott/pen/pgdPOb?editors=001
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Class Inheritance Example | |
| // NOT RECOMMENDED. Use object composition, instead. | |
| // https://gist.github.com/ericelliott/b668ce0ad1ab540df915 | |
| // http://codepen.io/ericelliott/pen/pgdPOb?editors=001 | |
| class GuitarAmp { | |
| constructor ({ cabinet = 'spruce', distortion = '1', volume = '0' } = {}) { | |
| Object.assign(this, { | |
| cabinet, distortion, volume | |
| }); | |
| } | |
| } | |
| class BassAmp extends GuitarAmp { | |
| constructor (options = {}) { | |
| super(options); | |
| this.lowCut = options.lowCut; | |
| } | |
| } | |
| class ChannelStrip extends BassAmp { | |
| constructor (options = {}) { | |
| super(options); | |
| this.inputLevel = options.inputLevel; | |
| } | |
| } | |
| test('Class Inheritance', nest => { | |
| nest.test('BassAmp', assert => { | |
| const msg = `instance should inherit props | |
| from GuitarAmp and BassAmp`; | |
| const myAmp = new BassAmp(); | |
| const actual = Object.keys(myAmp); | |
| const expected = ['cabinet', 'distortion', 'volume', 'lowCut']; | |
| assert.deepEqual(actual, expected, msg); | |
| assert.end(); | |
| }); | |
| nest.test('ChannelStrip', assert => { | |
| const msg = 'instance should inherit from GuitarAmp, BassAmp, and ChannelStrip'; | |
| const myStrip = new ChannelStrip(); | |
| const actual = Object.keys(myStrip); | |
| const expected = ['cabinet', 'distortion', 'volume', 'lowCut', 'inputLevel']; | |
| assert.deepEqual(actual, expected, msg); | |
| assert.end(); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment