LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2012, 03:03 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
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.
 
Old 10-03-2012, 05:54 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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().
 
Old 10-03-2012, 09:25 AM   #3
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
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?
 
Old 10-03-2012, 09:37 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
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.)
 
Old 10-04-2012, 06:30 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by resetreset View Post
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 View Post
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,

Last edited by Snark1994; 10-04-2012 at 06:32 AM.
 
Old 10-08-2012, 08:25 AM   #6
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
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?
 
Old 10-09-2012, 07:36 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by resetreset View Post
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) {...}
 
Old 10-09-2012, 07:42 AM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Ubuntu Edgy/Firefox 2/Javascript - Firefox closes accessing websites with Javascript Interdictor Ubuntu 8 11-02-2006 11:58 AM
If statement doesn't work properly in JavaScript Robhogg Programming 2 03-05-2006 03:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration