Reputation: 2906
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.
Upvotes: 0
Views: 84
Reputation: 382092
You probably don't do what you think you do.
The line var that = this;
is useless and this
, anyway, isn't really x but the receiver of the x.b
function.
This means that if you do
x.b();
this works
but if you do
var f = x.b;
f();
this doesn't work.
If you want to ensure that the working of x.b
isn't dependent of the receiver of the function, you may do this :
var x = function(){
var x ={
a: function() {
alert("hello");
}
};
x.b = function() {
mayhem(x.a);
}
return x;
}();
An alternative would be to create a constructor and make x using the new
operator.
Regarding you edit :
If you want main to be accessible, do this :
function StartObj() {
var events = {
a: function() {
alert("hello");
}
};
events.b = function() {
lightbox(events.a);
};
this.main = function() {
$("#objId").click(events.b);
}
}
Upvotes: 1
Reputation: 664185
You've screwed up your StartObj
constructor - it does not return an object with a main
method, main
is just a local function. Also, you've got a closing brace too much after the assignment of events.b
. This should work:
function StartObj() {
var events = {
a: function() {
alert("hello");
},
b: function() {
lightbox(events.a);
}
};
this.main = function main() {
$("#objId").click(events.b);
}
}
Also, make sure that lightbox
is really globally available - check your error console.
Upvotes: 0
Reputation: 8588
Are you sure? this is works for me... I just added the call to x.b()
to start it up
var x = {
a: function() {
alert("hello");
},
b: function() {
var that = this;
mayhem(that.a);
}
}
function mayhem(funcPtr)
{
funcPtr();
}
x.b();
Upvotes: 2