I'm learning my way through OOP in JavaScript and have hit a snag, when I create an instance of a Class I want to be able for it to run a set of internal functions.
From what I can tell it is similar to the idea of the init function in many other languages.
A Simple example would be:
function human(name, age){
this.name = name;
this.age = age;
this.init = function(){
console.log("hi my name is "+this.name+" and I am "+this.age+" years old.");
}//end of this.init
}//end of class human
var fred = new Human("fred", 30);
fred.init();
I want fred.init to run automatically without it having to be explicitly called. I understand it is an event so I thought of an EventListener something like:
this.addEventListener("initialization", this.init);
being put inside the class but I have not found the correct event. I have started to look into custom events but I am at a total loss here plus I still wouldn't know how to describe the initilisation even if I do wrap my head around it.
If someone could just point me in the right direction (if not the full solution) I would be most grateful.
thanks
Zen
p.s. a link to a custom events for dummies guide would also be nice
OK, so I am a bit of a dumbo, if anyone else has this problem, the solution is dead simple:
function human(name, age){
this.name = name;
this.age = age;
this.init = function(){
console.log("hi my name is "+this.name+" and I am "+this.age+" years old.");
}//end of this.init
this.init();
}//end of class human
var fred = new Human("fred", 30);
All you have to do is call the method inside of the class definition using the "this" iden