5

Can someone explain the difference in Javascript between:

var x = something

and

var x : something

I have no idea on where/how to search about it.

I saw the code above at the bottom of page 4 of this document: http://download.unity3d.com/support/Tutorials/2%20-%20Scripting%20Tutorial.pdf

Thanks in advance!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Saturnix
  • 10,130
  • 17
  • 64
  • 120
  • I think you are referring to object properties, yes? – Brad Nov 01 '12 at 20:50
  • 1
    See here: [What does ':' do in JavaScript?](http://stackoverflow.com/q/418799/102937) – Robert Harvey Nov 01 '12 at 20:51
  • 1
    nope, see here page 4 of this document, at the bottom. http://download.unity3d.com/support/Tutorials/2%20-%20Scripting%20Tutorial.pdf – Saturnix Nov 01 '12 at 21:01
  • 1
    I don't understand why this question has been closed. I still don't understand why 'var x : something' seems to work and what exactly it does. The link provided doesn't answer my question. – Saturnix Nov 01 '12 at 21:02
  • Your question was closed because your question has already been asked and answers on Stack Overflow. Look at the very top of your question and you'll see the link to the question you duplicated. – mah Nov 01 '12 at 21:44
  • 1
    @mah: unfortunately this question isn’t actually a duplicate of the suggested duplicate question. – Paul D. Waite Nov 01 '12 at 21:58

3 Answers3

9

The first one assigns something to a variable x and the other causes a syntax error.

You're probably mixing up assigning a property in an object literal and normal assignment.

var x = something;//assigning a variable
var y = {
    x:something//assigning a object property
};

Edit

var target : Transform;

seems to be UnityScript not JavaScript, it looks like it is not assigning a value but rather setting the variable type. see here

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Musa
  • 96,336
  • 17
  • 118
  • 137
  • 1
    page 4, at the bottom http://download.unity3d.com/support/Tutorials/2%20-%20Scripting%20Tutorial.pdf – Saturnix Nov 01 '12 at 20:58
  • so Unity uses a more sophisticated JavaScript? My bad, I should have told it was about Unity but I was thinking that was just plain JS. Thanks! – Saturnix Nov 01 '12 at 21:06
  • 1
    Dammit! I'm using JavaScript just to stay away from variables types declaration and then I discover this. Dang. Thanks a lot for the link! Now I understand. – Saturnix Nov 01 '12 at 23:10
3

UnityScript is not JavaScript

Unity Script vs Javascript

Cory Danielson
  • 14,314
  • 3
  • 44
  • 51
2

If you're defining vars in an object, you'd use colons.

var obj = {x:my_var};
lostPixels
  • 1,303
  • 9
  • 23