From 9fc1cb7b9d5619e993c9267819e42a9a6ccb7472 Mon Sep 17 00:00:00 2001 From: oddswop Date: Tue, 13 Dec 2016 06:47:56 +1100 Subject: [PATCH] Lesson 1 - constructors, prototypes and inheritance --- src/index.js | 68 +++++++++++++++++++++++++++++++++++++++++++ src/spec.js | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 149 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 5237ff4..9d75441 100644 --- a/src/index.js +++ b/src/index.js @@ -24,3 +24,71 @@ export function doubleEach ( arr ) { return result; } +// start simple + +//export function triple (x) { +// return x * 3; +//} +// +//export function tripleEach (arr) { +// let result = arr.map (triple); +// return result; +//} + +// lesson 1 - activity make Animal work. Constructors and Prototypes +// what I did: +// +//export function Animal ( animal,speak ) { +// this.speak = function(){ +// return "generic sound"; +// } +// +//} + +export function Animal (){ +} + +Animal.prototype.speak = function(){ + return 'generic sound'; +} + +// create new constructors with prototypes that inherit from Animal Reptile and Primate. Overwrite the speak method for each. + +export function Reptile(){ +// let SOUND = 'hiss'; +} + +Reptile.prototype = new Animal(); // inherit Animal properties if you get created +Reptile.prototype.constructor = Reptile; // Make a valid reptile which is a new animal +Reptile.SOUND = 'hissss'; +// you can create a function same as parent + +Reptile.prototype.speak = function(){ + return Reptile.SOUND; +} + +export function Primate(){ + } + +Primate.prototype = new Animal(); +Primate.prototype.constructor = Primate; +Primate.SOUND = "bongos"; + +Primate.prototype.speak = function (){ + return Primate.SOUND; +} + + +// Create a human + +export function Human(){ + +} + +Human.prototype = new Primate(); +Human.prototype.constructor = Human; +Human.message = 'hello'; + +Human.prototype.speak = function(){ + return Human.message; +} \ No newline at end of file diff --git a/src/spec.js b/src/spec.js index 3e22930..ae4ce38 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, tripleEach, Animal, Reptile, Primate, Human } from './index'; test( 'double fn', function ( test ) { const actual = double( 5 ); @@ -28,3 +28,83 @@ test( 'doubleEach', function ( test ) { test.end(); }); +// just checking if I can 'copy paste'! +// +//test ( 'tripleEach', function ( test ) { +// const actual = tripleEach([1, 3 ,6]); +// const expected = [3, 9, 18]; +// +// test.deepEqual( actual, expected, 'should triple each in the array'); +// +// test.end(); +//}); + +// lesson 1 activity - try and make test pass. + +test ( 'Animal', function ( test ) { + const animal = new Animal (); // constructor + + test.ok( animal instanceof Animal, 'should create an instance of Animal'); + +// console.log(animal); + + test.end(); +}); + +test ('Animal', 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(); +}); + + + +// inheritance +// +// + +test ('Inheritance', 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 ('Inheritance 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() +}); \ No newline at end of file