UnityScript versus JavaScript

From Unify Community Wiki
Revision as of 17:56, 7 April 2011 by Ppeterson (Talk | contribs)

Jump to: navigation, search

JavaScript is class-free

JavaScript has no classes. This is because it's a prototypal language, and not a classical one. Inheritance happens with [more dynamic] objects, rather than [unchanging] classes. <javascript>function Machine(x) {

  this.kind = ["bulldozer", "lathe", "car"][x];

} var c = new Machine(2); print(typeof c.announce); // "undefined"

Machine.prototype.announce = function() {

  print("I am a "+this.kind+".");


print(typeof c.announce); // "function" c.announce();</javascript>

Dynamic typing is inefficient

This code is valid in both UnityScript and JavaScript: <javascript>var x; x = 3;</javascript> However, it is inefficient in UnityScript because it causes x to be dynamically typed. For faster runtime execution, use one of the two static typing syntaxes. <javascript>var x = 3; // type `int` is inferred, typed statically</javascript>

<javascript>var x : int; // typed statically x = 3;</javascript>


In JavaScript, privacy is rather unconventional.

<javascript>function Person() {

  var secret = "I am a mass murderer."; // private
  this.speak = function() { print("Don't make me tell you my secret! "+secret); }; // prints secret

} var bob = new Person(); print(bob.secret); // undefined</javascript>

Personal tools