From 723898704316a2d84e26bcd8dcdac132c368d5c8 Mon Sep 17 00:00:00 2001 From: Urmila Pradeep Date: Wed, 14 Dec 2016 15:45:54 -0800 Subject: [PATCH 1/4] First attempt at prototypes assignment --- src/index.js | 37 ++++++++++++----------- src/spec.js | 85 +++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 87 insertions(+), 35 deletions(-) diff --git a/src/index.js b/src/index.js index 5237ff4..27b6419 100644 --- a/src/index.js +++ b/src/index.js @@ -1,26 +1,29 @@ -export function double ( x ) { - return x * 2; +export function Animal () { + } -export function doubleXTimes ( x, num ) { - let result = x; +Animal.prototype.speak = function() { + return 'generic sound'; +} - for ( let i = 1; i <= num; i++ ) { - // result = result * 2; - result = double( result ); - } - return result; +export function Reptile() { + this.SOUND = 'reptile sound'; } -export function doubleEach ( arr ) { - let result = arr.map( double ); +export function Primate() { + this.SOUND = 'primate sound' +} - // const result = arr.map( x => double( x ) ); - // const result = arr.map( function ( x ) { - // return double( x ); - // }); +Reptile.prototype = Object.create(Animal.prototype) - return result; -} +Reptile.prototype.speak = function () { + return this.SOUND; +}; + +Primate.prototype = Object.create(Animal.prototype) + +Primate.prototype.speak = function (sound) { + return this.SOUND; +} diff --git a/src/spec.js b/src/spec.js index 3e22930..5c2712a 100644 --- a/src/spec.js +++ b/src/spec.js @@ -1,30 +1,79 @@ -import test from 'tape'; -import { double, doubleXTimes, doubleEach } from './index'; +// import test from 'tape'; +// import { double, doubleXTimes, doubleEach } 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( 'double fn', function ( test ) { - const actual = double( 5 ); - const expected = 10; +import test from 'tape'; +import {Animal, Reptile, Primate} from './index'; - test.equal( actual, expected, 'should double the value' ); +test('instance', function (test) { + const animal = new Animal(); + test.ok( animal instanceof Animal, 'should create an instance of Animal' ); - test.end(); + test.end(); }); -test( 'doubleXTimes', function ( test ) { - const actual = doubleXTimes( 5, 3 ); - const expected = 40; +test('sound', function (test) { - test.equal( actual, expected, 'should double 5 three times' ); + const animal = new Animal(); + const expected = 'generic sound'; + const actual = animal.speak('generic sound'); - test.end(); + test.equal( actual, expected, 'should make a generic sound when it speaks' ); + test.end(); }); -test( 'doubleEach', function ( test ) { - const actual = doubleEach([ 0, 1, 2 ]); - const expected = [ 0, 2, 4 ]; +test('reptile', function (test) { - test.deepEqual( actual, expected, 'should double each in the array' ); + let actual, expected; - test.end(); -}); + 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' ); + test.end(); +}) + +test('primate', function (test) { + + 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(); +}); From c00e836d530c9457609d5640b917aebfef8f65fe Mon Sep 17 00:00:00 2001 From: Urmila Pradeep Date: Wed, 14 Dec 2016 16:02:20 -0800 Subject: [PATCH 2/4] Removed old code --- src/spec.js | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/src/spec.js b/src/spec.js index 5c2712a..5fd2cda 100644 --- a/src/spec.js +++ b/src/spec.js @@ -1,33 +1,3 @@ -// import test from 'tape'; -// import { double, doubleXTimes, doubleEach } 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(); -// }); - import test from 'tape'; import {Animal, Reptile, Primate} from './index'; From bb2d8a1cc554fd2be346a5392364574d3cb049a3 Mon Sep 17 00:00:00 2001 From: Urmila Pradeep Date: Wed, 14 Dec 2016 21:46:45 -0800 Subject: [PATCH 3/4] Finally all tests pass with ES5 for class 1 assignemnt --- src/index.js | 12 ++++++++++-- src/spec.js | 25 ++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 27b6419..6bb260c 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ export function Reptile() { } export function Primate() { - this.SOUND = 'primate sound' + this.SOUND = 'primate sound'; } Reptile.prototype = Object.create(Animal.prototype) @@ -24,6 +24,14 @@ Reptile.prototype.speak = function () { Primate.prototype = Object.create(Animal.prototype) -Primate.prototype.speak = function (sound) { +Primate.prototype.speak = function () { return this.SOUND; } + +export function Human() {} + +Human.prototype = Object.create(Primate.prototype) + +Human.prototype.speak = function (word) { + return word +} diff --git a/src/spec.js b/src/spec.js index 5fd2cda..960ba82 100644 --- a/src/spec.js +++ b/src/spec.js @@ -1,5 +1,5 @@ import test from 'tape'; -import {Animal, Reptile, Primate} from './index'; +import {Animal, Reptile, Primate, Human} from './index'; test('instance', function (test) { const animal = new Animal(); @@ -27,7 +27,7 @@ test('reptile', function (test) { 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' ); test.end(); @@ -35,15 +35,34 @@ test('reptile', function (test) { test('primate', function (test) { + let actual, expected; 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; + expected = primate.SOUND; actual = primate.speak(); test.equal( actual, expected, 'should make a primate sound when it speaks' ); test.end(); }); + +test('human', 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(); +}); From 880c0cdf1353f27df000f108374b7dbbef3bc553 Mon Sep 17 00:00:00 2001 From: Urmila Pradeep Date: Wed, 14 Dec 2016 22:09:08 -0800 Subject: [PATCH 4/4] Assignment using ES6 class --- src/index.js | 89 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 25 deletions(-) diff --git a/src/index.js b/src/index.js index 6bb260c..97e29e9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,37 +1,76 @@ -export function Animal () { - -} - -Animal.prototype.speak = function() { - return 'generic sound'; -} +// export function Animal () { +// +// } +// +// Animal.prototype.speak = function() { +// return 'generic sound'; +// } +// +// +// export function Reptile() { +// this.SOUND = 'reptile sound'; +// } +// +// export function Primate() { +// this.SOUND = 'primate sound'; +// } +// +// Reptile.prototype = Object.create(Animal.prototype) +// +// Reptile.prototype.speak = function () { +// return this.SOUND; +// }; +// +// +// Primate.prototype = Object.create(Animal.prototype) +// +// Primate.prototype.speak = function () { +// return this.SOUND; +// } +// +// export function Human() {} +// +// Human.prototype = Object.create(Primate.prototype) +// +// Human.prototype.speak = function (word) { +// return word +// } +// IN ES6 +export class Animal { + constructor() {}; -export function Reptile() { - this.SOUND = 'reptile sound'; + speak() { + return 'generic sound'; + } } -export function Primate() { - this.SOUND = 'primate sound'; -} - -Reptile.prototype = Object.create(Animal.prototype) +export class Reptile extends Animal { -Reptile.prototype.speak = function () { - return this.SOUND; -}; + constructor() { + super(); + this.SOUND = 'reptile sound' + }; -Primate.prototype = Object.create(Animal.prototype) - -Primate.prototype.speak = function () { - return this.SOUND; + speak() { + return this.SOUND; + } } -export function Human() {} +export class Primate extends Animal { + constructor() { + super(); + this.SOUND = 'primate sound' + }; -Human.prototype = Object.create(Primate.prototype) + speak() { + return this.SOUND; + } +} -Human.prototype.speak = function (word) { - return word +export class Human extends Primate { + speak(words) { + return words; + } }