the javascript language
BY SA
Javascript is not Java
Brendan Eich at Netscape in 1995 named in deal between Netscape and Sun ECMA standard calls it ECMAScript
only language in all modern web browsers currently focus of lots of optimization work
the DOM (Document Object Model, the hierarchy of objects representing page content in web browsers) AJAX (Asynchronous Javascript and XML, a technique to submit/request data without refreshing the page)
dynamic and (a little) functional object-oriented programming via prototyping
numbers booleans strings arrays (lists) objects (dictionaries) functions null
+ addition - subtraction/negation / division *multiplication %modulus
x * ((x * 3) + 9) (+ (* x 3) 9)
foo(a, b, c) bar() (foo a b c) (bar)
foo (foo) ((foo)) foo foo() (foo()) (foo)
foo = bar / 2 (foo = (bar / 2)) (= foo (/ bar 2)) lvalue rvalue
x = y = z = (x = (y = (z = (4 + 2)))) (((x = y) = z) = (4 + 2)) the = operator is right-to-left associative
x = 3; cow = 2 + x; foo(1); rat = bar();
free-form syntax x = 3; x = 3 ;
foo(); // ignore this /* ignore all this too */ bar();
/* apple /* banana orange */ lemon */
_foo foo_bar $foo foo$bar
var var name; var name = expression; var monkey; var zebra = 3;
var jeff; foo(jeff); // OK foo(amanda); // error undefined
function function(parameters) {body}
! not === equals !== not equals &&and ||or < less than > greater than <= less than or equal >= greater than or equal
!true// false !false// true !null// true !0// true !“”// true !undefined// true
3 === 3// true 3 === -2// false !true === false// true
(3 === 3) || (2 > 4) // true
if / else if (condition) {body} else if (condition) {body} else {body} while while (condition) {body}
if (x === 3) { foo(); } else { bar(); }
var i = 0; while (i < 5) { foo(); i = i + 1; }
var foo = function() { return bar; }; foo(); // error var bar = 3; foo(); // OK
var foo = function() { bar = 6; return bar; var bar; }; foo(); // 6
nested functions
AC AD B E B AD CB C
closure
objects {properties} object[string]
var foo = {}; var bar = {“bill”: 1 + 1, “diana”: 11}; var ack = bar[“bill”]; // 2 bar[“bill”] = 3; foo[“ned”] = 8; ack = foo[“ted”]; // undefined
var foo = {}; var bar = {bill: 1 + 1, diana: 11}; var ack = bar.bill; // 2 bar.bill = 3; foo.ned = 8; ack = foo.ted; // undefined
methods var foo = {}; foo.bar = function() { this.ack = 3; }; foo.bar();
var foo = {}; foo.bar = function(x) { x.ack = 3; }; foo.bar(foo);
var foo = {}; foo.bar = function() { this = 3; //error }; foo.bar();
var bar = function() { return this; }; bar(); // global object passed to this
object links AC AD B E B AD CB C
var foo = {a: 1, b: 2, c: 3}; var bar = {a: 4, d: 5}; var ack = {b: 6, e: 7}; … // foo bar ack var x = foo.c === bar.c === ack.c; // true bar.c = 8; x = ack.c; // 8 x = foo.c; // 3
var foo = “hello”; var x = foo.length; // 5 x = foo.charAt(1) ; // “e” x = “avast”.charAt(3) ; // “s”
charAt lengthcharAtlengthcharAt
var Tom = function(foo, bar) { this.foo = foo; this.bar = bar; }; var jane = (new Tom(2, 3)); var david = new Tom(7, 10);
var Tom = function(foo, bar) { this.foo = foo; this.bar = bar; }; Tom.prototype.ack = “hello”; var jane = new Tom(2, 3); var x = jane.ack; // “hello” ack foo ack bar
arrays [items] array[index]
var foo = []; var bar = [4, “hello”, 33]; var ack = bar[2]; // 33 bar[3] = “orange”;
var foo = []; var bar = [4, “hello”, 33]; var ack = bar[“2”]; // 33 bar[“3”] = “orange”;
var foo = []; var bar = [4, “hello”, 33]; var ack = foo.length; // 0 ack = bar.length; // 3 bar[15] = true; ack = bar.length; // 16 ack = bar[8]; // undefined
arguments var foo = function(a, b, c) { // arguments[0] equals a // arguments[1] equals b // arguments[2] equals c … };
var foo = function(a, b, c) { … }; foo(1, 7, 11, 2, 8); // OK foo(6, 7); // OK
var sum = function() { var i = 0; var sum = 0; while (i < arguments.length) { sum = sum + arguments[i]; i = i + 1; } return sum; }; sum(2, 2, 3); // 7 sum(5, 7); // 12 sum(); // 0
exceptions foo(“hello” * true);
exceptions try { foo(“hello” * true); bar(); } catch (ex) { ack(ex); }
try { throw “We’re screwed.”; bar(); } catch (ex) { ack(ex); }
var foo = function () { … // exception }; foo(); // exception
try { foo(); // exception bar(); } catch (ex) { ack(); }
var bar = function() { try { foo(); // exception } catch (ex) { ack(); } foo(); // exception }; bar(); // exception