1

Hi can someone tell me why I can't instantiate the following class/object?

function Arbitrage(odds1, odds2, investment)
{
    this.investment = investment;
    this.odds1 = odds1;
    this.odds2 = odds2;

    this.arbPercent = function() {
        return 1.0/this.odds1 + 1.0/this.odds2;
    };

    this.profit = function() {
        return this.investment / this.arbPercent() - this.investment;
    };

    this.individualBets = function() {
        return    {
                odds1bet : this.investment/this.odds1/this.arbPercent(),
                odds2bet : this.investment/this.odds2/this.arbPercent()    
            };
    };
};

module.exports = Arbitrage;

I'm calling it like this:

var utility = require('../businesslogic/utility');
...
router.post('/calculate', function(req, res)
    {
        var arbit = new utility.Arbitrage(req.body.odds1, req.body.odds2, req.body.investment);
        res.json({
            arbPercentage : arbit.arbPercent(),
            profit : arbit.Profit(),
            indvBets : arbit.individualBets()
        });
    });

The first line, var arbit = new utility.Arbitrage(...) is throwing the error. It says TypeError: undefined is not a function

I've checked that utility isn't null or anything like that. Also all the constructor arguments are ok.

I'm not very familiar with javascript, any help would be appreciated. Thanks.

1
  • What is the name of the file where you have defined Arbitrage? Commented Aug 17, 2014 at 0:15

2 Answers 2

3

You're exporting your Arbitrage class directly, so after you're requiring it

var utility = require('../businesslogic/utility');

utility is actually your Arbitrage class, meaning that typeof utility === 'function'.

I can see two ways of fixing it.

1. Change the way you're requiring your Arbitrage class:

var Arbitrage = require('../businesslogic/utility');
// ...
var arbit = new Arbitrage(...);

2. Or change the way you're exporting it:

exports.Arbitrage = Arbitrage;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that did it. If I wanted to export utility...as a namespace...is that possible? Would I have to keep adding each new function I add in there to exports.[name] individually?
@shek you could prefix each new function definition with exports.[name] = instead of exporting all your functions at the end of your module, e.g. exports.Arbitrage = function Arbitrage(...) {....
You can import multiple files in your utility file and export them a separate objects. var AnotherUtility = require('./AnotherUtility.js'); exports.AnotherUtility = AnotherUtility
-1

it's because of the way you exported it

you should use :

module.exports.Arbitrage = Arbitrage;

And then you can instanciate like this :

var Arbitrage = require('../businesslogic/utility');
var varbitrage = new Arbitrage();

1 Comment

It he'll do both things he'll get object is not a function error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.