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?
The call() allows for a function/method belonging to one object to be assigned and called for a different object. call() provides a new value of this to the function/method. With call() , you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.
Yes, it is possible, when your constructor function executes, the this value has already the [[Prototype]] internal property pointing to the ValidateFields.
When a function is created in JavaScript, the JavaScript engine adds a prototype property to the function. This prototype property is an object (called a prototype object) that has a constructor property by default. The constructor property points back to the function on which prototype object is a property.
constructor. The constructor property returns a reference to the Object constructor function that created the instance object. Note that the value of this property is a reference to the function itself, not a string containing the function's name.
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();
There are a few things wrong with your Code
1.initialiseWebGl()
would look for a function declared in the Global scope -> there is no function
this.initialiseWebGl()
to access the Objects Methodthis
refers to the Instance of Renderer
in this case 2.You are not assigning a function with Renderer.prototype.initialiseWebGL()
instead you try to invoke the Renderer
s prototype method initialiseWebGl
which gives you an error, as its not defined
3.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 in Uncaught SyntaxError: Unexpected token {
Heres an Commented Example
function Renderer() { initialiseWebGL(); // I call the global declared function this.initialiseShader(); //I call the Prototypes function this.initialiseBuffer(); //Me too } Renderer.prototype.initialiseWebGL = function (){ //Here a function gets assigned to propertie of `Renderer`s `prototype` Object //Do stuff. }; Renderer.prototype.initialiseShader = function (){ console.log("Do Shader Stuff"); }; Renderer.prototype.initialiseBuffer = function (){ console.log("Do initialise stuff"); }; Renderer.prototype.initialiseBuffer() // I invoke the method above { console.log("I'm a Block statement"); }; function initialiseWebGL () { //I'm the global declared function console.log("Global"); } var ren1 = new Renderer(); /*"Do initialise stuff" "I'm a Block statement" "Global" "Do Shader Stuff" "Do initialise stuff"*/
As you can see in the consoles Output
Heres a JSBin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With