I'm new to JavaScript still, I would say, so I think that the ways I work on projects now are kind of shaping my way of thinking about how things should be done in js. I have understood that one should work preferably with modules when programming in js. This has led me to think about events in js. For example say I have this object Monkey
:
function Monkey(color, position){
this.color = color;
this.position = position;
this.jumpAround = function() {
/* jumps around and screams */
};
}
And say I have built a whole app like this, with monkeys
, giraffes
and shih tzus
, that interact in a webapp. How should the events then be handled? Are you left to just implement callbackfunctions in a global namespace? Like:
//objects
var monkey = new Monkey(brown, pos);
var giraffe = new Giraffe(yellow, pos);
var shih_tzu = new Shih_tzu(white, pos);
//event handlers
this_and_that.onclick = function() { /* this and that happens */ }
...
And then include this file in the html header? Maybe this is a silly question with an obvious answer, but still too me it seems as if there aren't any good best practices.