LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What does this statement mean in Javascript? (https://www.linuxquestions.org/questions/programming-9/what-does-this-statement-mean-in-javascript-4175430234/)

resetreset 10-03-2012 03:03 AM

What does this statement mean in Javascript?
 
What is this line doing?:

collider = SYS_collisionManager.newCollider(PLAYER, ALIEN_BOMB, 30, 12, that.hit) ;


that.hit has been previously defined like this:

that.hit = function() {

//whatever





I would like to specifically know what's going on with the "that.hit" argument above.


Thanks.

Snark1994 10-03-2012 05:54 AM

I don't know what library you're using, but it looks like it's being used as a callback: when PLAYER and ALIEN_BOMB collide (which uses the numbers 30 and 12 - perhaps defining the bound box around 'PLAYER'? You'd have to look up the docs for the function) run the function that.hit().

resetreset 10-03-2012 09:25 AM

It's not a library, it's a paper book I'm reading.
Yes, I think you're right, but could you explain to me how "that.hit" WOULD be set up as a callback?

sundialsvcs 10-03-2012 09:37 AM

When a collision occurs, that routine would be invoked with some set of parameters so that you could remove the poor player, add an explosion, deduct lives or points. (Or maybe it's the other way around ... remove the poor alien bomb, add an explosion, add points. After all, it's your game.)

Snark1994 10-04-2012 06:30 AM

Quote:

Originally Posted by resetreset (Post 4796044)
It's not a library, it's a paper book I'm reading.

Well... You're using a SYS_collisionManager class, which is either in a library or something you've defined but haven't told us about - all I was saying is that "I have no idea what that code is meant to do, but I'm making guesses based on what it looks like it should do".

Quote:

Originally Posted by resetreset (Post 4796044)
Yes, I think you're right, but could you explain to me how "that.hit" WOULD be set up as a callback?

You're essentially moving it into the scope of the function, as a variable. I haven't actually tested this code, so if it doesn't work tell me and I'll try to get it to do so, but the basic idea is:

Code:

function doSomething(callback){
    callback("http://www.google.com");
}

doSomething(alert);
doSomething(function(s){ window.location.href = s; });

The first one calls 'alert' on the string "http://www.google.com" (displaying it in a popup) while the second runs an anonymous function (I can't remember if it's normal to call them lambda functions here) which sets window.localtion.href to "http://www.google.com" (redirecting the user to google).

EDIT: I stopped being lazy and checked that my code works, which it does. Any problems are therefore of your own devising :P

Hope this helps,

resetreset 10-08-2012 08:25 AM

Sorry for the late reply, thanks a lot for your reply Snark, but I didn't get both lines of your code:
For "doSomething(alert);" , it means you're calling doSomething with an arg of alert, right? ie. it expects that there's a variable called alert, whose value will be passed to doSomething?
NOT the Javascript built-in FUNCTION alert, which will popup a window. So how does it work then?


And for "doSomething(function(s){ window.location.href = s; });" - well I just don't get it.
You're including the function's code as an arg to doSomething? How does that work?

By the way, could you recommend a good book on all this stuff? Is "Javascript: A Definitive Guide" a good one?

ntubski 10-09-2012 07:36 AM

Quote:

Originally Posted by resetreset (Post 4800220)
For "doSomething(alert);" , it means you're calling doSomething with an arg of alert, right? ie. it expects that there's a variable called alert, whose value will be passed to doSomething?
NOT the Javascript built-in FUNCTION alert, which will popup a window. So how does it work then?

You seem to have the idea that functions are different from values, but this is not the case. Other languages do make this distinction, but in JavaScript the story goes like this: there is a variable called alert whose value is a function that makes a popup window.

Quote:

And for "doSomething(function(s){ window.location.href = s; });" - well I just don't get it.
You're including the function's code as an arg to doSomething? How does that work?
Same here, a value (which happens to be a function) is being passed as an arg to doSomething.

Code:

// Instead of writing
function doSomething(callback) {...}
// you could say
doSomething = function(callback) {...}


Snark1994 10-09-2012 07:42 AM

No idea RE the books, sorry. I've just picked up javascript as I go along, really (I don't do all that much of it anyway).

The long and short of it, as ntubski said, is that functions are values just as 2 and "red" and True are - so they can be passed as variables.


All times are GMT -5. The time now is 03:32 PM.