0

I'm very new to JavaScript and thought a good assignment would be working through Kent Beck's TDD By Example and doing it in JavaScript instead of Java. Simple inheritance seems to be mystifying as there are many opinions on how to achieve this, such as this stackoverflow entry: JavaScript Inheritance

I don't want to imitate inheritance in C++/Java, and even if I eventually use a library, I want to know how to implement it properly myself. Please look at the following simple currency example from TDD By Example rewritten in JavaScript and Jasmine and, disregarding the triviality of the code, tell me if this is a proper technique.

// currency.js

var CommonCurrency = function() {

    function Currency(amount){
        this.amount = amount;
    }

    Currency.prototype.times = function(multiplier){
        return new Currency(this.amount*multiplier);
    };

    function Dollar(amount){
        Currency.call(this, amount);
    }
    Dollar.prototype = Object.create(Currency.prototype);
    Dollar.prototype.constructor = Dollar;

    function Pound(amount){
        Currency.call(this, amount);
    }
    Pound.prototype = Object.create(Currency.prototype);
    Pound.prototype.constructor = Pound;

    return {
        Dollar: Dollar,
        Pound: Pound 
    }
}();

module.exports = CommonCurrency;

spec file:

// spec/currency-spec.js

var currency = require("../currency");

describe("testCurrency", function() {
    describe("testDollar", function() {     
        var fiveDollars = new currency.Dollar(5);

        it("should multiply dollar amount by given parameter", function() {
            var product = fiveDollars.times(2);
            expect(product.amount).toBe(10);
        });

        it("should return new dollar amount and not multiply last result ", function() {
            var product = fiveDollars.times(3);
            expect(product.amount).toBe(15);
        });
    });

    describe("testPound", function() {
        var fivePounds;

        it("should multiply pound amount by given parameter", function() {
            var product = fivePounds.times(2);
            expect(product.amount).toBe(10);
        });

        it("should return new pound amount and not multiply last result ", function() {
            var product = fivePounds.times(3);
            expect(product.amount).toBe(15);
        });
    });  
});

Thanks.

Community
  • 1
  • 1
gzulux
  • 1
  • 1
  • 1
    The best way to perform inheritance in JavaScript is to avoid it. It's usually unnecessary, and none of the built-in utilities to make it work are very helpful. You can always hack it together with ES5 features, but even then it's usually easier to stick to a more functional style of programming in JS. – Alexis King Dec 21 '14 at 00:09
  • 1
    I disagree that it should be avoided. There are places where it can be very useful. Not everywhere, but everything in its place. You might want to check out the [MDN article on Object Oriented JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript). – Alexander O'Mara Dec 21 '14 at 00:14
  • Aren't `Dollar` and `Pound` the same? – Oriol Dec 21 '14 at 00:51
  • at this point, yeah. – gzulux Dec 21 '14 at 00:54

0 Answers0