0

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.

1
  • How are you calling it? These are just definitions. (I think your issue might be with what this means inside your b function.) Commented Oct 11, 2012 at 15:54

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not calling x.b straight away. I'm initializing an object and then I'm calling.
1

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);
   }
}

7 Comments

Can you plz review my question ?
See edit. But what do you want to alert ? the function code or just "hello" ?
Actually, the parameter I'm passing inside the function is a callback function which will be called when the lightbox initializes itself. I'm just checking with a function stub.
@dystroy: Your last code with the prototype doesn't work, because events is local to the constructor
@Bergi Right : too many copy-paste... As it's yet confused enough and probably not relevant I remove the prototype version...
|
0

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.

1 Comment

Hey. Thanks for the reply dude. I"ve put the closing bracket properly. But when pasting here, I made a mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.