I’ve created an object as follows.
function StartObj() {
var events = {
a: function() {
alert("hello");
},
b = function() {
lightbox(events.a);
}
};
this.main = function() {
$("#objId").click(events.b);
}
}
$(document).ready(function(){
var _start_obj = new StartObj();
_start_obj.main();
});
And in another file,
function lightbox(funcPtr) {
alert(funcPtr);
}
The alert is reporting funcPtr is undefined; also the google chrome console.
You probably don’t do what you think you do.
The line
var that = this;is useless andthis, anyway, isn’t really x but the receiver of thex.bfunction.This means that if you do
this works
but if you do
this doesn’t work.
If you want to ensure that the working of
x.bisn’t dependent of the receiver of the function, you may do this :An alternative would be to create a constructor and make x using the
newoperator.Regarding you edit :
If you want main to be accessible, do this :