views:

153

answers:

3

I come from other programming languages and I am new to Javascript. I am trying to understand what a Javascript code is doing.

I found this line

var guiTouchPos : Vector2 = touch.position - guiTouchOffset;

What kind of declaration is this? What is this line doing? Wouldn't it be easy to write

var guiTouchPos = touch.position - guiTouchOffset;

as I don't see Vector2 being used anywhere in the code?

is the line assigning the subtraction to both variables?

thanks.

+4  A: 

That's invalid syntax in JavaScript, so as written that cannot work.

UPDATE

The syntax is valid in ActionScript, and the part immediately after the colon (Vector2 here) indicates the type of the variable being declared; it's also apparently valid and means the same thing in the JavaScript-like scripting language in Unity, which it seems from Chuck's answer is apparently what the code was originally written for. From what I've just read about Unity's "JavaScript", it's not compatible with any version of the language and should not be called JavaScript.

Tim Down
Downvoter: please explain. The question has now been retagged as ActionScript but at the time I answered, this was correct.
Tim Down
they call it Javascript...
Digital Robot
@Digital Robot: Yes, and that's what I'm objecting to. I'd prefer it if they stuck to calling it UnityScript. It's quite like JavaScript but also does its own thing (e.g. no anonymous functions) which means that it doesn't conform to any definition of JavaScript.
Tim Down
Yes, I discovered that when I tried to googled for that kind of declaration using the keyword javascript and found nothing! :D
Digital Robot
+10  A: 

That is not JavaScript, it's ActionScript 3 or UnityScript. the : indicates a type declaration.

apphacker
Exactly what i thought....
st0le
I think a similar deceleration scheme was also considered for the ECMAScript 4 standard but that effort was abandoned.
Alexandre Jasmin
ActionScript 3 is the basis for ecma 4
apphacker
It's actually ECMAScript 4 upon which ActionScript 3 (and 2) are based. It is also, therefore, part of JavaScript 2 so the statement isn't exactly correct.
Lazarus
Lazarus is wrong. ECMAScript4 has been abandoned. No browser or current JavaScript compiler will recognize that as valid syntax, neither Gecko, V8, Webkit, nor Rhino or Triton.
apphacker
Except that no browser i know of actually uses Javascript 2, leaving only possibly node.js and other arcane environments that don't really matter yet.
cHao
@cHao - Again... who said anything about browsers? Jeez.
Lazarus
That is not valid syntax in node.js which uses v8. It will produce "SyntaxError: Unexpected token :" in node.js
apphacker
@apphacker - Ever thought of looking further than the browser? Unity uses it and the example the OP has given is from Unity. And whether implemented, in development or abandoned, it **is** still valid JavaScript 2.0!
Lazarus
@Lazarus: Until it's blessed by ECMA, there is no "valid JavaScript 2.0!". There's stuff that implements what would have been in JS2, but "valid" implies conformance to some standard, which does not exist.
cHao
@Lazarus Yes it's UnityScript I came to the same conclusion but you beat me to it. I will add an unity tag to the question.
Alexandre Jasmin
@cHao - Since when has ECMA been responsible for JavaScript?
Lazarus
@apphacker - ECMAScript 4 has been scaled back and renamed Harmony but hardly abandoned. Harmony still includes classes.
Lazarus
@Lazarus: Since Javascript has become all but a direct translation of the ECMAScript docs. ECMAScript even started out as JS, but got its name changed when it was standardized.
cHao
@cHao - And still ECMA do not validate or approve JavaScript, it doesn't matter how you dress it up.
Lazarus
@cHao - I expect you will back me up that there's no such thing as valid HTML5 based on your arguments.
Lazarus
@Lazarus: Psh. Show me Javascript (real JS, not that "new, improved" non-standard flavor you keep going on about) that's not also valid ECMAScript...or ECMAScript that's not also valid JS. The two differ in very little but the name.
cHao
@Lazarus: There isn't, til it's published somewhere (not in a draft, but in a real finished document) exactly what is and isn't HTML 5. That doesn't stop it from being useful, though, as most browsers implement it. *Unlike* this "valid JavaScript 2.0!" crap, which no browser and no common interpreter implements.
cHao
@cHao - Ah... it has to be common implementation for it to be valid against a draft standard. Blimey, you are strict.
Lazarus
@Lazarus: Yeah, cause i was around for the later part of the browser wars. Incompatibilities all over the place. It still isn't totally over (witness IE), but it's a hell of a lot better than it used to be. What i'm against is a return to that state of things, where everyone had their own little custom twist on everything. Standards are useful, even de facto standards, as they emphasize compatibility.
cHao
In fact *Actionscript*, *JScript.NET* and *UnitityScript* all share this optional type deceleration scheme that was considered for ES4. I'm not going to debate if these language can be rightfully called Javascript or ECMAScript. This debate doesn't bring anything useful.
Alexandre Jasmin
Well I actually prefer static typing so if new versions of JavaScript end up supporting : I would be happy. In any case, the question has been answered several times over.
apphacker
@apphacker I'm sorry if I'm being pedantic but could you edit your post to remove the assertion that *"it's ActionScript 3"* since the OP code clearly is UnityScript.
Alexandre Jasmin
@Alexandre, done.
apphacker
+2  A: 

Vector2 is a type declaration : http://www.cs.sjsu.edu/~rucker/asteroids/html/Vector2.html

Are you writing something for iOS? http://forum.unity3d.com/threads/51609-How-to-make-character-JUMP

Chuck