Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Passing function to other prototype

Passing function to other prototype

Problem

I have below code

http://jsfiddle.net/qhoc/SashU/1/

var Callback = function(op) {
    this.callback = op.callback;
}

var Test = function (op) {
  for (var option in op) {
    if (!this[option]) this[option] = op[option];
  }

}

Test.prototype.init = function(bb) {
    console.log('aa = ' + this.aa);
    console.log('bb = ' + bb);

    if (bb 

Line 19 didn't print 'this is finalCallback' AT ALL because this.callback; got executed but it doesn't point to a function. But the below works:

myTest.init(1);
myCallback.callback();

I guess when passing myCallback to myTest, it didn't pass finalCallback??

Can someone help to explain this behavior and how to fix it?

Problem courtesy of: HP.

Solution

seems you want to make this (use op.callback as function ):

var Callback = function (op) {
    this.callback = op.callback;
    return this.callback;
};

and functions should be invoked with ()

 } else {
        this.callback();
  }

http://jsfiddle.net/oceog/SashU/3/

this is example where it can be used

Solution courtesy of: zb'

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Passing function to other prototype

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×