Javascript calling prototype functions in constructor

Danny picture Danny · Jan 21, 2013 · Viewed 35.3k times · Source

I keep getting an error saying that my functions are not defined when I was trying to call the prototype functions in the constructor and I dont know whats wrong with it.

Here's the code I have:

function Renderer()
{
    initialiseWebGL();
    initialiseShader();
    initialiseBuffer();
}

Renderer.prototype.initialiseWebGL()
{
    //Do stuff.
};

Renderer.prototype.initialiseShader()
{
        //Do Shader's stuff
};

Renderer.prototype.initialiseBuffer()
{
        //Do Buffers
};

What is wrong with it?

Answer

Minko Gechev picture Minko Gechev · Jan 21, 2013

Your syntax is wrong. Use this:

function Renderer() {
    this.initialiseWebGL();
    this.initialiseShader();
    this.initialiseBuffer();
}

Renderer.prototype.initialiseWebGL = function () {
    //Do stuff.
};

Renderer.prototype.initialiseShader = function () {
        //Do Shader's stuff
};

Renderer.prototype.initialiseBuffer = function () {
        //Do Buffers
};

After that you can create new object and use it by:

var rendererInstance = new Renderer();