From 21a7564968db25a15802de48175c43e68f48f320 Mon Sep 17 00:00:00 2001 From: Justin Reid Date: Mon, 12 Dec 2016 00:02:13 -0500 Subject: [PATCH 1/6] First attempt at inheritance homework. --- .gitignore | 1 + src/index.js | 31 +++++++++++++++++++++------- src/spec.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c2658d7..78f2710 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +.idea/ diff --git a/src/index.js b/src/index.js index 5237ff4..2a7b54d 100644 --- a/src/index.js +++ b/src/index.js @@ -13,14 +13,31 @@ export function doubleXTimes ( x, num ) { return result; } -export function doubleEach ( arr ) { - let result = arr.map( double ); +export const doubleEach = x => x.map(double); - // const result = arr.map( x => double( x ) ); - // const result = arr.map( function ( x ) { - // return double( x ); - // }); +export function Animal() { + this.SOUND = 'generic sound'; +} +Animal.prototype.speak = function () {return this.SOUND}; - return result; +export function Reptile() { + this.SOUND = 'Roar'; +} + +Reptile.prototype = new Animal(); +Reptile.prototype.constructor = Reptile; + +export function Primate() { + this.SOUND = 'Ooh Ooh Eee!'; } +Primate.prototype = new Animal(); +Primate.prototype.constructor = Primate; + +export function Human() { + this.SOUND = 'hello'; +} +Human.prototype = new Primate(); +Human.prototype.constructor = Human; + + diff --git a/src/spec.js b/src/spec.js index 3e22930..1fb1a27 100644 --- a/src/spec.js +++ b/src/spec.js @@ -1,5 +1,5 @@ import test from 'tape'; -import { double, doubleXTimes, doubleEach } from './index'; +import { double, doubleXTimes, doubleEach, Animal, Reptile, Primate, Human} from './index'; test( 'double fn', function ( test ) { const actual = double( 5 ); @@ -28,3 +28,59 @@ test( 'doubleEach', function ( test ) { test.end(); }); +test('Test to see if a new "Animal" instance can be created', function (test) { + const animal = new Animal(); + test.ok( animal instanceof Animal, 'should create an instance of Animal'); + test.end(); +}); + +test('Test to see if the Animal type has a "speak" method', function (test) { + + const animal = new Animal(); + const expected = 'generic sound'; + const actual = animal.speak(); + + test.equal( actual, expected, 'should make a generic sound when it speaks' ); + test.end(); +}); + +test('Test to see if "Primates" and "Reptiles" can be made from "Animals"', function (test) { + + let actual, expected; + + const reptile = new Reptile(); + + test.ok( reptile instanceof Reptile, 'should be an instance of Reptile' ); + test.ok( reptile instanceof Animal, 'should be an instance of Animal' ); + + expected = reptile.SOUND; + actual = reptile.speak(); + test.equal( actual, expected, 'should make a reptile sound when it speaks' ); + + const primate = new Primate(); + + test.ok( primate instanceof Primate, 'should be an instance of Primate' ); + test.ok( primate instanceof Animal, 'should be an instance of Animal' ); + + expected = primate.SOUND; + actual = primate.speak(); + test.equal( actual, expected, 'should make a primate sound when it speaks' ); + + test.end(); +}); + +test('Test to see if a human can be made and it can speak', function (test) { + + let actual, expected; + + const human = new Human(); + + test.ok( human instanceof Human, 'should be an instance of Human' ); + test.ok( human instanceof Primate, 'should be an instance of Primate' ); + test.ok( human instanceof Animal, 'should be an instance of Animal' ); + + const message = 'hello'; + expected = message; + actual = human.speak( message ); + test.equal( actual, expected, 'should speak what it is asked to speak' ); +}); \ No newline at end of file From 7e5daf19ee2e4f63c304b89b9c4ce41713fb7ac6 Mon Sep 17 00:00:00 2001 From: Justin Reid Date: Mon, 12 Dec 2016 01:01:39 -0500 Subject: [PATCH 2/6] Second attempt at inheritance homework. This addresses the issue of attributes bound to each object instead of their constructors. --- src/index.js | 14 +++++++++----- src/spec.js | 5 +++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 2a7b54d..64d3767 100644 --- a/src/index.js +++ b/src/index.js @@ -16,26 +16,30 @@ export function doubleXTimes ( x, num ) { export const doubleEach = x => x.map(double); export function Animal() { - this.SOUND = 'generic sound'; + this.clorophyll = false; } -Animal.prototype.speak = function () {return this.SOUND}; +Animal.SOUND = 'generic sound'; +Animal.prototype.speak = function () {return this.constructor.SOUND}; export function Reptile() { - this.SOUND = 'Roar'; + this.scales = true; } +Reptile.SOUND = 'Roar'; Reptile.prototype = new Animal(); Reptile.prototype.constructor = Reptile; export function Primate() { - this.SOUND = 'Ooh Ooh Eee!'; + this.thumbs = true; } +Primate.SOUND = 'Ooh ooh eee!'; Primate.prototype = new Animal(); Primate.prototype.constructor = Primate; export function Human() { - this.SOUND = 'hello'; + this.sentience = true; } +Human.SOUND = 'hello'; Human.prototype = new Primate(); Human.prototype.constructor = Human; diff --git a/src/spec.js b/src/spec.js index 1fb1a27..f7cbe84 100644 --- a/src/spec.js +++ b/src/spec.js @@ -53,7 +53,7 @@ test('Test to see if "Primates" and "Reptiles" can be made from "Animals"', func test.ok( reptile instanceof Reptile, 'should be an instance of Reptile' ); test.ok( reptile instanceof Animal, 'should be an instance of Animal' ); - expected = reptile.SOUND; + expected = Reptile.SOUND; actual = reptile.speak(); test.equal( actual, expected, 'should make a reptile sound when it speaks' ); @@ -62,7 +62,7 @@ test('Test to see if "Primates" and "Reptiles" can be made from "Animals"', func test.ok( primate instanceof Primate, 'should be an instance of Primate' ); test.ok( primate instanceof Animal, 'should be an instance of Animal' ); - expected = primate.SOUND; + expected = Primate.SOUND; actual = primate.speak(); test.equal( actual, expected, 'should make a primate sound when it speaks' ); @@ -83,4 +83,5 @@ test('Test to see if a human can be made and it can speak', function (test) { expected = message; actual = human.speak( message ); test.equal( actual, expected, 'should speak what it is asked to speak' ); + test.end(); }); \ No newline at end of file From 75fcdfe7c5f823852c820a9c3ecadf0411553014 Mon Sep 17 00:00:00 2001 From: Justin Reid Date: Mon, 12 Dec 2016 01:26:32 -0500 Subject: [PATCH 3/6] Third attempt at inheritance homework. This time making unique speak methods for Animal and its sub-types. --- src/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 64d3767..2c05330 100644 --- a/src/index.js +++ b/src/index.js @@ -19,7 +19,7 @@ export function Animal() { this.clorophyll = false; } Animal.SOUND = 'generic sound'; -Animal.prototype.speak = function () {return this.constructor.SOUND}; +Animal.prototype.speak = function () {return Animal.SOUND}; export function Reptile() { this.scales = true; @@ -28,6 +28,7 @@ export function Reptile() { Reptile.SOUND = 'Roar'; Reptile.prototype = new Animal(); Reptile.prototype.constructor = Reptile; +Reptile.prototype.speak = function () {return Reptile.SOUND}; export function Primate() { this.thumbs = true; @@ -35,6 +36,7 @@ export function Primate() { Primate.SOUND = 'Ooh ooh eee!'; Primate.prototype = new Animal(); Primate.prototype.constructor = Primate; +Primate.prototype.speak = function () {return Primate.SOUND}; export function Human() { this.sentience = true; @@ -42,6 +44,6 @@ export function Human() { Human.SOUND = 'hello'; Human.prototype = new Primate(); Human.prototype.constructor = Human; - +Human.prototype.speak = function () {return Human.SOUND}; From 6f7c2f7f8992c3273bef3ce88adef42379332144 Mon Sep 17 00:00:00 2001 From: Justin Reid Date: Mon, 12 Dec 2016 01:49:37 -0500 Subject: [PATCH 4/6] 4th Attempt at Inheritance homework. Refactored code to use Object.create() and made constructors empty. --- src/index.js | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/src/index.js b/src/index.js index 2c05330..15f002b 100644 --- a/src/index.js +++ b/src/index.js @@ -15,35 +15,23 @@ export function doubleXTimes ( x, num ) { export const doubleEach = x => x.map(double); -export function Animal() { - this.clorophyll = false; -} +export function Animal(){} Animal.SOUND = 'generic sound'; Animal.prototype.speak = function () {return Animal.SOUND}; -export function Reptile() { - this.scales = true; -} - +export function Reptile(){} Reptile.SOUND = 'Roar'; -Reptile.prototype = new Animal(); -Reptile.prototype.constructor = Reptile; +Reptile.prototype = Object.create(Animal.prototype); Reptile.prototype.speak = function () {return Reptile.SOUND}; -export function Primate() { - this.thumbs = true; -} +export function Primate(){} Primate.SOUND = 'Ooh ooh eee!'; -Primate.prototype = new Animal(); -Primate.prototype.constructor = Primate; +Primate.prototype = Object.create(Animal.prototype); Primate.prototype.speak = function () {return Primate.SOUND}; -export function Human() { - this.sentience = true; -} +export function Human(){} Human.SOUND = 'hello'; -Human.prototype = new Primate(); -Human.prototype.constructor = Human; +Human.prototype = Object.create(Primate.prototype); Human.prototype.speak = function () {return Human.SOUND}; From daa7404d5476c9c811ca8fc9b688a6749ac6052f Mon Sep 17 00:00:00 2001 From: Justin Reid Date: Tue, 13 Dec 2016 23:52:46 -0500 Subject: [PATCH 5/6] Implemented the same functionality as with original code, but this time with ES6 classes. --- src/class_spec.js | 64 ++++++++++++++++++++++++++++++++++++++++++++ src/index_classes.js | 42 +++++++++++++++++++++++++++++ src/spec.js | 28 +------------------ 3 files changed, 107 insertions(+), 27 deletions(-) create mode 100644 src/class_spec.js create mode 100644 src/index_classes.js diff --git a/src/class_spec.js b/src/class_spec.js new file mode 100644 index 0000000..de7c0e0 --- /dev/null +++ b/src/class_spec.js @@ -0,0 +1,64 @@ +/** + * Created by towershine on 12/13/16. + */ +import test from 'tape'; +import {Animal, Reptile, Primate, Human} from './index_classes'; + + +test('Test to see if a new "Animal" instance can be created', function (test) { + const animal = new Animal(); + test.ok( animal instanceof Animal, 'should create an instance of Animal'); + test.end(); +}); + +test('Test to see if the Animal type has a "speak" method', function (test) { + + const animal = new Animal(); + const expected = 'generic sound'; + const actual = animal.speak(); + + test.equal( actual, expected, 'should make a generic sound when it speaks' ); + test.end(); +}); + +test('Test to see if "Primates" and "Reptiles" can be made from "Animals"', function (test) { + + let actual, expected; + + const reptile = new Reptile(); + + test.ok( reptile instanceof Reptile, 'should be an instance of Reptile' ); + test.ok( reptile instanceof Animal, 'should be an instance of Animal' ); + + expected = Reptile.SOUND; + actual = reptile.speak(); + test.equal( actual, expected, 'should make a reptile sound when it speaks' ); + + const primate = new Primate(); + + test.ok( primate instanceof Primate, 'should be an instance of Primate' ); + test.ok( primate instanceof Animal, 'should be an instance of Animal' ); + + expected = Primate.SOUND; + actual = primate.speak(); + test.equal( actual, expected, 'should make a primate sound when it speaks' ); + + test.end(); +}); + +test('Test to see if a human can be made and it can speak', function (test) { + + let actual, expected; + + const human = new Human(); + + test.ok( human instanceof Human, 'should be an instance of Human' ); + test.ok( human instanceof Primate, 'should be an instance of Primate' ); + test.ok( human instanceof Animal, 'should be an instance of Animal' ); + + const message = 'hello'; + expected = message; + actual = human.speak( message ); + test.equal( actual, expected, 'should speak what it is asked to speak' ); + test.end(); +}); \ No newline at end of file diff --git a/src/index_classes.js b/src/index_classes.js new file mode 100644 index 0000000..e1644c6 --- /dev/null +++ b/src/index_classes.js @@ -0,0 +1,42 @@ +export class Animal { + constructor () {} + + static SOUND = 'generic sound'; + + speak() { + return Animal.SOUND; + } +} + +export class Reptile extends Animal { + constructor () { + super(); + } + + static SOUND = 'Roar'; + + speak() { + return Reptile.SOUND; + } +} + +export class Primate extends Animal { + constructor() { + super(); + } + + static SOUND = 'Ooh Ooh Eeh!'; + speak() { + return Primate.SOUND; + } +} + +export class Human extends Primate { + constructor() { + super(); + } + static SOUND = 'hello'; + speak() { + return Human.SOUND; + } +} \ No newline at end of file diff --git a/src/spec.js b/src/spec.js index f7cbe84..bbe9f5b 100644 --- a/src/spec.js +++ b/src/spec.js @@ -1,32 +1,6 @@ import test from 'tape'; -import { double, doubleXTimes, doubleEach, Animal, Reptile, Primate, Human} from './index'; +import {Animal, Reptile, Primate, Human} from './index'; -test( 'double fn', function ( test ) { - const actual = double( 5 ); - const expected = 10; - - test.equal( actual, expected, 'should double the value' ); - - test.end(); -}); - -test( 'doubleXTimes', function ( test ) { - const actual = doubleXTimes( 5, 3 ); - const expected = 40; - - test.equal( actual, expected, 'should double 5 three times' ); - - test.end(); -}); - -test( 'doubleEach', function ( test ) { - const actual = doubleEach([ 0, 1, 2 ]); - const expected = [ 0, 2, 4 ]; - - test.deepEqual( actual, expected, 'should double each in the array' ); - - test.end(); -}); test('Test to see if a new "Animal" instance can be created', function (test) { const animal = new Animal(); From cd1b8ff2c7fa303f975995cab3695a1b9c34cffe Mon Sep 17 00:00:00 2001 From: Justin Reid Date: Thu, 15 Dec 2016 10:04:24 -0500 Subject: [PATCH 6/6] Changed human method so that it accepts a parameter and not an constant. --- src/index.js | 5 +---- src/index_classes.js | 5 ++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 15f002b..d35435f 100644 --- a/src/index.js +++ b/src/index.js @@ -30,8 +30,5 @@ Primate.prototype = Object.create(Animal.prototype); Primate.prototype.speak = function () {return Primate.SOUND}; export function Human(){} -Human.SOUND = 'hello'; Human.prototype = Object.create(Primate.prototype); -Human.prototype.speak = function () {return Human.SOUND}; - - +Human.prototype.speak = function (message) {return message}; diff --git a/src/index_classes.js b/src/index_classes.js index e1644c6..3b56de9 100644 --- a/src/index_classes.js +++ b/src/index_classes.js @@ -35,8 +35,7 @@ export class Human extends Primate { constructor() { super(); } - static SOUND = 'hello'; - speak() { - return Human.SOUND; + speak(message) { + return message; } } \ No newline at end of file