Javascript
Javascript
Javascript
JavaScript
John Mitchell
Adapted by Mooly Sagiv
Architect Programmer
Programming
Language Compiler,
Q/A Runtime
Testing environ-
ment
Diagnosti
cTools
Language syntax
• JavaScript is case sensitive
– HTML is not case sensitive; onClick, ONCLICK, … are HTML
• Statements terminated by returns or semi-colons (;)
– x = x+1; same as x = x+1
– Semi-colons can be a good idea, to reduce errors
• “Blocks”
– Group statements using { … }
– Not a separate scope, unlike other languages (see later slide)
• Variables
– Define a variable using the var statement
– Define implicitly by its first use, which must be an assignment
• Implicit definition has global scope, even if it occurs in nested scope
Stand-alone implementation
• Spidermonkey command-line interpreter
– Read-eval-print loop
• Enter declaration or statement
• Interpreter executes
• Displays value
• Returns to input state
– Example
• Catch
try { …
} catch (e if e == “FirstException") { // do something
} catch (e if e == “SecondException") { // do something else
} catch (e){ // executed if no match above
}
Reference: http://developer.mozilla.org/en/docs/
Core_JavaScript_1.5_Guide :Exception_Handling_Statements
Object features
• Dynamic lookup
– Method depends on run-time value of object
• Encapsulation
– Object contains private data, public operations
• Subtyping
– Object of one type can be used in place of another
• Inheritance
– Use implementation of one kind of object to
implement another kind of object
Concurrency
• JavaScript itself is single-threaded
– How can we tell if a language provides concurrency?
• AJAX provides a form of concurrency
– Create XMLHttpRequest object, set callback function
– Call request method, which continues asynchronously
– Reply from remote site executes callback function
• Event waits in event queue…
– Closures important for proper execution of callbacks
• Another form of concurrency
– use SetTimeout to do cooperative multi-tasking
• Maybe we will explore this in homework …
Unusual features of JavaScript
• Some built-in functions
– Eval (next slide), Run-time type checking functions, …
• Regular expressions
– Useful support of pattern matching
• Add, delete methods of an object dynamically
– Seen examples adding methods. Do you like this? Disadvantages?
– myobj.a = 5; myobj.b = 12; delete myobj.a;
• Redefine native functions and objects (incl undefined)
• Iterate over methods of an object
– for (variable in object) { statements }
• With statement (“considered harmful” – why??)
– with (object) { statements }
JavaScript eval
• Evaluate string as code
– The eval function evaluates a string of JavaScript code, in
scope of the calling code
• Examples
var code = "var a = 1";
eval(code); // a is now '1‘
var obj = new Object();
obj.eval(code); // obj.a is now 1
• Most common use
– Efficiently deserialize a large, complicated JavaScript data
structures received over network via XMLHttpRequest
• What does it cost to have eval in the language?
– Can you do this in C? What would it take to implement?
Other code/string conversions
• String computation of property names
• In addition
• for (p in o){....}
• o[p]
• eval(...)
allow strings to be used as code and vice versa
Lessons Learned
• Few constructs make a powerful language
• Simplifies the interpreter
• But the interaction can be hard to understand
for programmers
– JSLint
• Hard for compilation, verification, …
References
• Brendan Eich, slides from ICFP conference talk
• Tutorial
– http://www.w3schools.com/js/
• JavaScript 1.5 Guide
– http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide
• Douglas Crockford
– http://www.crockford.com/JavaScript/
– JavaScript: The Good Parts, O’Reilly, 2008. (book)
– David Flanagan
– JavaScript: The Definitive Guide O’Reilly 2006 (book)
– Ankur Taly
– An Operational Semantics for JavaScript