Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Unity JS have a class?

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?

like image 993
Moon Avatar asked Sep 07 '12 08:09

Moon


People also ask

How do classes work in Unity?

Classes are the blueprints for your objects. Basically, in Unity, all of your scripts will begin with a class declaration. Unity automatically puts this in the script for you when you create a new C# script. This class shares the name as the script file that it is in.

Why did Unity stop using JavaScript?

It's a proprietary language and it doesn't actually follow any concrete specification of standard JavaScript and is modified at will by the engine developers. > Because of this, the vast majority of JavaScript libraries you find will not work by default in Unity.

Was JavaScript removed from Unity?

Also, that Javascript is no longer supported on Unity3d.

Is C# better than JavaScript for Unity?

Go with c#, unityscript is slooooooooowwwwwwlllyyyy going the way of the dodo. All the newer unity framework documentation and tutorials is in c#, better overall support from msdn and core library docs, better library support. Used outside of unity etc.


2 Answers

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"

like image 150
Bentleyo Avatar answered Sep 20 '22 03:09

Bentleyo


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.

like image 35
Chchwy Avatar answered Sep 20 '22 03:09

Chchwy