6

I was reading an article on Unity JS. The article showed an example of a class in unity js.

class Person{
    var name;
    var career;
}

//Create objects of type Person
var john = Person();
john.name = "John Smith";
john.career = "doctor";
Debug.Log(john.name + " is a " + john.career);

It looks like Unity JS actually supports the class keyword. How does it support class with JavaScript? Are they using dead ES4 spec or a modified ES3 or 5?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Moon
  • 22,195
  • 68
  • 188
  • 269

2 Answers2

9

Check out this article on the differences between UnityScript and JavaScript: http://wiki.unity3d.com/index.php?title=UnityScript_versus_JavaScript

In addition: Wikipedia describes UnityScript as "a custom language with ECMAScript-inspired syntax"

Bentleyo
  • 106
  • 3
  • Welcome to Stack Overflow! A short summary of the linked-to content would greatly improve this answer (and help prevent problems if the link goes stale at a later time). – Joachim Sauer Sep 07 '12 at 08:15
  • 1
    For the purposes of this question, I think the most important phrase from that article is 'Unity's ".js" language doesn't even come close to conforming to that [the ECMAScript] specification'. – James Allardice Sep 07 '12 at 08:17
  • A short summary would be nice as Joachim Saucer pointed out, but I guess I have to accept it as an answer. The information on the link is accurate. – Moon Sep 07 '12 at 08:41
1

Unity's Javascript is not real Javascript, it's a new language for beginners with similar syntax. Some people prefer to call it UnityScript because they're just two different languages.

Unity's Javascript is a class-based OO language which supports all OOP features, and it can be compiled to the same assembly code just as C# in Unity.

for example, a javascript file "Foo.js" in Unity

var myVar : int;
function Update() {}

is identical to a C# script like

class Foo : MonoBehaviour {
    int myVar;
    void Update() {}
}

I mean 100% identical even though you can't find the keyword 'class' in Foo.js, but you are actually creating a Foo object.

you can inherit class Foo in another script "Boo.js" to verify what I say:

class Boo extends Foo {
    function Update() {}
}

Unity's javascript has javscript's syntax, but C#'s spirit.

Chchwy
  • 326
  • 3
  • 9