Jump to content

.add(listener,listenerContext) - what is the reason why we can't directly call a function with parameters?


valueerror
 Share

Recommended Posts

i'm just trying to get more insight in how things work and why they work the way they do..

 

because of the limitation described in the thread title i need to do things like

xy.add(function(){ mystuff goes here});

OR i pass an object as context and fetch it in my function

xy.add( myglobalfunction, object);  // object = this;

all of this feels very much like a workaround.. a hack... i would rather do something like this:

xy.add( myglobalfunction(param1,param2) );

i am creating closures all over my code and functions that live for a millisecond ...  the only purpose of this i can see is to provide enough work for the garbage collector.

 

i'm somewhat of a javascript noob and i would very much appreciate if someone could clarifiy this for me.. thank you in advance :)

 

 

Link to comment
Share on other sites

all of this feels very much like a workaround.. a hack... i would rather do something like this:

xy.add( myglobalfunction(param1,param2) );

i am creating closures all over my code and functions that live for a millisecond ...  the only purpose of this i can see is to provide enough work for the garbage collector.

 

i'm somewhat of a javascript noob and i would very much appreciate if someone could clarifiy this for me.. thank you in advance :)

 

You can't do that, that code calls myglobalfunction() the moment that line is executed, and passes the value that function returns to xy.add() as it's first parameter.

 

If you modified Phaser perhaps you could change the add() method to support a forth parameter, a hash of key-value pairs to be passed to the callback as it's argument list as in -

xy.add( myglobalfunction, null, null, {"x":param1, "y":param2} );

but is that really that much better?

Link to comment
Share on other sites

What you are thinking of is Function.prototype.bind():

xy.add( myglobalfunction.bind(object, param1, param2) );  // object = this;
The other way you see often in Phaser is so that you don't have to call `bind`, it isn't hacky just another way to do things. It isn't native to `addEventListener` but is implemented in Phaser.
Link to comment
Share on other sites

You can't do that, that code calls myglobalfunction() the moment that line is executed, and passes the value that function returns to xy.add() as it's first parameter.

 

ay..  now i get it..    functionname();   runs that function.. i knew that but i didn't think it would also run "inside" the .add( )   -- thank you!

 

 

@xerver..    i just read more about the .bind() on mozilla.org   - this sounds very interesting  - thank you too :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...