By: Daniel Henneberger
Introduction Data Types Flow Control Inheritance/Built-in Functions User defined types, Generics Arrays Function methods Quirks Comparison
Prototype-based scripting language Interpreted? Chrome V8 attempts to compile Java’s Rhino attempts to compile to byte code Mozilla attempts JIT Garbage Collection – Up to implementer Loosely-typed, First-class functions, dynamic Java-like syntax Two Subsets Default Language (will be abandon later) Strict Language Free The word JavaScript must be licensed by Oracle Mozilla has ‘Inventor rights’ Brief
Developed for client-side processing on web browsers Now capable of other tasks Web Servers (node.js) Desktop Widgets Inside Applications Applications
Originally Called LiveScript Created for Netscape Navigator by Brendan Eich Renamed JavaScript Most likely a marking ploy Microsoft created Jscript Formalized to ECMAScript ISO/IEC ECMAScript v2 ECMAScript v3 History of JavaScript
– ECMAScript v4 (Abandoned) Google’s V8 Engine Node.js created ECMAScript v5 Test262 created ISO/IEC 16262:2011 ECMAScript v5.1 History of JavaScript
2013 – Java 8’s JavaScript Engine (Nashorn) 201? ECMAScript v6 Only strict mode Incompatible syntax Classes Getters/Setters Tamper proof objects Block Scoping Future of JavaScript
Adobe, Mozilla, Microsoft Later came IBM, Google, Apple, Opera, Dojo, and Company 100. ECMAScript Committee
CodenameBrowserTest262 Fail Percent JscriptMicrosoft Internet Explo. 95.2% SpiderMonkeyFirefox1.5% FutharkOpera0.1% V8Chrome0.1% Implementations Test262 really important
End of line semicolons optional Automatically inserted by compiler Excess whitespace and comments are discarded during lexical analysis Strings can be defined by ' or " Multiline Strings possible with \ Grammar
Introduction Data Types Flow Control Inheritance/Built-in Functions User defined types, Generics Arrays Function methods Quirks Comparison
var - has functional scoping const null undefined let has block scoping ( ECMAScript v6 ) let and const to replace var Usable Types
Undefined - primitive value used when a variable has not been assigned a value Null - no value Boolean – true/false Number - double-precision 64-bit NaN, Positive Infinity, Negative Infinity IEEE 754 String - UTF-16 Object runtime system performs automatic type conversion as needed 1+true=2, null + false = 0, null != false, null == undefined Type coercion – new Boolean(true); Internal Types
Introduction Data Types Flow Control Inheritance/Built-in Functions User defined types, Generics Arrays Function methods Quirks Comparison
Flow Control - Sequencing function test(){ for( var i = 0; i<10; i++){ //doSomething; } return i; } result is 10
if ( Expression ) Statement else Statement if ( Expression ) Statement SwitchStatement: switch ( Expression ) CaseBlock CaseBlock: { CaseClause } { CaseClause DefaultClause CaseClause } CaseClause: case Expression : StatementList Default Clause: default : StatementList with( Expression ) Statement LogicalORExpression ? AssignmentExpression : AssignmentExpression Flow Control – Selection
Simple Assignment: = Compound Assignment: op= *= /= %= += -= >= >>>= &= ^= |= Comma Operator (, ) Flow Control – Expression Evaluation
do Statement while ( Expression ); while ( Expression ) Statement for ( ExpressionNoIn ; Expression ; Expression ) Statement for ( var VarDeclaration ; Expression ; Expression ) Statement for ( LeftHandSideExpression in Expression ) Statement for ( var VariableDeclarationNoIn in Expression ) Statement label, break, continue, (throw, return, ‘normal’) Flow Control – Iteration
function Identifier ( ParameterList ) { FunctionBody } function ( ParameterList ) { FunctionBody } function square(y){ this.rslt = y*y; } var x = new square(5); x.rslt; var square = new Function("a", "b", "return a*b") Flow Control – Procedural Abstraction var x = function(y) { return y * y; }; x(5); function square(y){ return y*y; } square(5); var Math = { square: function(y){ return y*y } Math.square(5);
Object Properties Flow Control – Procedural Abstraction Object.defineProperty(o, "a", { value : 37, writable : true, enumerable : true, configurable : true, get : function(){ return bValue; }, set : function(newValue){ bValue = newValue; } });
No optimization for Tail Call Recursion No concurrency setTimeout(func, delay, [params]) setInterval() new Worker(“something.js”) Flow Control – Recursion/Concurrency
try / catch try / finally try / catch / finally try { Code } catch ( Identifier ) {Code} finally {Code} throw Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError and URIError Flow Control – Exception Handling
Global - variables that are not declared but are referenced are automatically global Eval Strict Mode- cannot return variables Creates a new binding Function function x(){t = 5}; x(); console.log(t) //5 function x(){var t = 5}; x(); console.log(t) //undef Execution Context
Introduction Data Types Flow Control Inheritance/Built-in Functions User defined types, Generics Arrays Function methods Quirks Comparison
function Person(){} function Ninja(){} Ninja.prototype = new Person(); (new Ninja()) instanceof Person var o = new Object(); o.[[Prototype]] = Foo.prototype; o.Foo(); Prototype Based Inheritance
Number parseInt() parseFloat() parseInt() isNaN() ifFinite() eval() Library Math JSON Date RegExp Special Characters decodeURI encodeURI Built-in Functions
Introduction Data Types Flow Control Inheritance/Built-in Functions User defined types, Generics Arrays Function methods Quirks Comparison
user defined types generics NONE!
Introduction Data Types Flow Control Inheritance/Built-in Functions User defined types, Generics Arrays Function methods Quirks Comparison
new Array([item1, item2, …]) new Array(len) Keys can be integers or strings Can be treated as: Arrays, Queues, Stacks, Priority Queues, Set No bounds arr[arr.length] = I; Arrays
push pop reverse sort sort(function) shift slice splice unshift indexOf every lastIndexOf some(function) forEach(function) map(function) filter(function) reduce(function) reduceRight(function) Array Operations
x = 42; // creates the property x on the global object var y = 43; // declares a new variable, y myobj = {}; myobj.h = 4; // creates property h on myobj myobj.k = 5; // creates property k on myobj delete x; // returns true (x is a property of the global object and can be deleted) delete y; // returns false (delete doesn't affect variable names) delete Math.PI; // returns false (delete doesn't affect certain predefined properties) delete myobj.h; // returns true (user-defined properties can be deleted) with(myobj) { delete k; // returns true (equivalent to delete myobj.k) } delete myobj; // returns true delete
Introduction Data Types Flow Control Inheritance/Built-in Functions User defined types, Generics Arrays Function methods Quirks Comparison
bind() is powerful x.bind(this); call() – allows calling inner functions with current bindings toString() Built-in Function Functions
Introduction Data Types Flow Control Inheritance/Built-in Functions User defined types, Generics Arrays Function methods Quirks Comparison
Floating point arithmetic IEEE 754 !== 0.3 A bad choice All implementations see to behave differently Code not portable Accidental scoping Global variables var a = 0 || "4"; a = "4“ Scope hoisting eval() Quirks
Introduction Data Types Flow Control Inheritance/Built-in Functions User defined types, Generics Arrays Function methods Quirks Comparison
JavaScriptJavaCGo Type Loosely Typed Strongly Typed Inheritance PrototypalClassical Automatic Multiple Inheritance No Yes Generics NoYesNo Pointers NoYesNoYes Closures YesNo Yes Exceptions Yes NoBuilt-in Threads NoYes Garbage Collection Yes NoYes Semi-Colon Optional YesNo Yes Like JavaScript 100%30% 40%
“Java and Javascript are similar like Car and Carpet are similar.” Greg Hewgill
/publications/files/ECMA-ST/Ecma-262.pdf References