How do I call class methods from functions within the class? My code is:
var myExtension = {
init: function() {
// Call onPageLoad
},
onPageLoad: function() {
// Do something
},
}
I’ve tried …
onPageLoad();
… from within the init method but it’s saying it’s not defined.
I’m not having much luck with google because I don’t understand the syntax used. All the JS OOP examples I find online are in a different format. I’m using the format Mozilla use for extension development.
Assuming that you have called
initlike this:then it should be:
But in Javascript functions are not actually bound to objects and you can call any function on any other object, like this:
In this example
thiswithininitwould beanotherObject, which doesn’t haveonPageLoaddefined. If you want to support this kind of usage you’ll have to manually reference the initial object: