Search code examples
javascriptclosuresunobtrusive-javascript

Why does it say xxx is not a function


why is this not ok?

aContract = function(){};
aContract.prototype = {
    someFunction: function() {
        alert('yo');
    },
    someOtherFunction: some$Other$Function
};

var some$Other$Function = function() {
    alert('Yo yo yo');
};

var c = new aContract();
c.someFunction();
c.someOtherFunction();

Firebug says c.someOtherFunction is not a function

But this works just fine

aContract = function(){};
aContract.prototype = {
    someFunction: function() {
        alert('yo');
    },
    someOtherFunction: some$Other$Function
};

function some$Other$Function() {
    alert('Yo yo yo');
};

var c = new aContract();
c.someFunction();
c.someOtherFunction();

What am I missing here??? I prefer to code in javascript using the first method, which usually works fine, but doesn't seem to work correctly when I prototype.

Thanks, ~ck in Sandy Eggo


Solution

  • You have assigned some$Other$Function to aContract.prototype.someOtherFunction before you actually create some$Other$Function. The order of statements matters. If you switch the order of things you'll be good:

    var some$Other$Function = function() {
        alert('Yo yo yo');
    };
    
    aContract = function(){};
    aContract.prototype = {
        someFunction: function() {
            alert('yo');
        },
        someOtherFunction: some$Other$Function
    };