740 likes | 895 Views
http:// proglit.com /. the javascript language. SA. BY. 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.
E N D
the javascript language
SA BY
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 AJAX (Document Object Model, the hierarchy of objects representing page content in web browsers) (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 * 3 + 9 ((x * 3) + 9) (+ (* x 3) 9)
foo(a, b, c) bar() (foo a b c) (bar)
foo() (foo()) (foo) foo (foo) ((foo)) foo
lvalue rvalue foo = bar / 2 (foo = (bar / 2)) (= foo (/ bar 2))
x = y = z = 4 + 2 (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 ; x = 3 ;
foo(); // ignore this /* ignore all this too */ bar();
_foo foo_bar $foo foo$bar
var varname; varname = expression; var monkey; var zebra = 3;
undefined varjeff; foo(jeff); // OK foo(amanda); // error
function function(parameters) {body}
var helen = function(apple, orange) { return apple + orange; }; var dan = helen(3, 5);
! 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
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 var foo = function(a) { var bar = function(b) { returnb + 3; }; returnbar(a); }; foo(5); // 8
Function A B C Function A D B C Function E C B D A
var foo = function(a) { var bar = function(a) { returna + 3; }; returnbar(a); }; foo(5); // 8
var foo = function() { vara = 3; var bar = function(b) { returna + b; }; a = 5; returnbar(6); }; foo(); // 11
closure var foo = function(a) { var b = 4; return function(c) { returna + b + c; }; }; varbar = foo(2); var ack = foo(3); bar(6); // 12 ack(6); // 13
closure var foo = function(a) { return function() { a = a + 2; returna; }; }; varbar = foo(2); var ack = foo(3); bar(); // 4 bar(); // 6 ack(); // 5
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();