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?
Your syntax is wrong. Use this:
After that you can create new object and use it by:
There are a few things wrong with your Code
1.
initialiseWebGl()
would look for a function declared in the Global scope -> there is no functionthis.initialiseWebGl()
to access the Objects MethodNote:
this
refers to the Instance ofRenderer
in this case2.You are not assigning a function with
Renderer.prototype.initialiseWebGL()
instead you try to invoke theRenderer
s prototype methodinitialiseWebGl
which gives you an error, as its not defined3.Because the
{
are moved down a line they get interpreted as a Block -> this code gets executed.If you'd had them after your
()
you would get a Syntax Error ->Renderer.prototype.initialiseWebGL() {...
would result inUncaught SyntaxError: Unexpected token {
Heres an Commented Example
As you can see in the consoles Output
Heres a JSBin
Since your instances inherit the (method) properties from the prototype objects, you need to access them as properties and not as plain variables: