Programming Language-Javascript
Programming Language-Javascript
JavaScript
John Mitchell
This example uses the browser Document Object Model (DOM). For now,
we will focus on JavaScript as a language, not its use in the browser.
Design goals
Brendan Eich’s 2006 ICFP talk
• Make it easy to copy/paste snippets of code
• Tolerate “minor” errors (missing semicolons)
• Simplified onclick, onmousedown, etc., event
handling, inspired by HyperCard
• Pick a few hard-working, powerful primitives
– First class functions for procedural abstraction
– Objects everywhere, prototype-based
• Leave all else out!
Language basics
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?
Useful implementation
Spidermonkey command-line interpreter
• Read-eval-print loop
– Enter declaration or statement
– Interpreter executes
– Displays value
– Returns to input state
• Example
= x. (x+1)+1
var o = { x: 10
f : function() {
function g(){ return this.x } ;
return g();
}
};
o.f()
Reference http://www.unix.org.ua/orelly/web/jscript/ch11_07.html
Discuss garbage collection in connection with Lisp
Closures
Return a function from function call
function f(x) {
var y = x;
return function (z){y += z; return y;}
}
var h = f(5);
h(3);
Can use this idea to define objects with “private” fields
• Description of technique
– http://www.crockford.com/JavaScript/private.html
– http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Working_
with_Closures
• But there are subtleties (look for __parent__)
Exceptions
Throw an expression of any type
throw "Error2";
throw 42;
throw {toString: function() { return "I'm an object!"; } };
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 …
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?
Unusual features of JavaScript
Some built-in functions
• Eval, 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 }
References
Brendan Eich, slides from ICFP conference talk
• www.mozilla.org/js/language/ICFP-Keynote.ppt
Tutorial
• http://www.w3schools.com/js/ (still there?)
JavaScript 1.5 Guide
• http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_G
uide
Douglas Crockford site
• http://www.crockford.com/JavaScript/
• http://20bits.com/2007/03/08/the-philosophy-of-JavaScript/