Presentation is loading. Please wait.

Presentation is loading. Please wait.

the javascript language BY SA.

Similar presentations


Presentation on theme: "the javascript language BY SA."— Presentation transcript:

1 http://proglit.com/

2 the javascript language

3 BY SA

4

5 Javascript is not Java

6 Brendan Eich at Netscape in 1995 named in deal between Netscape and Sun ECMA standard calls it ECMAScript

7 only language in all modern web browsers currently focus of lots of optimization work

8 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)

9 dynamic and (a little) functional object-oriented programming via prototyping

10 numbers booleans strings arrays (lists) objects (dictionaries) functions null

11 http://proglit.com/

12 + addition - subtraction/negation / division *multiplication %modulus

13 x * 3 + 9 ((x * 3) + 9) (+ (* x 3) 9)

14 foo(a, b, c) bar() (foo a b c) (bar)

15 foo (foo) ((foo)) foo foo() (foo()) (foo)

16 foo = bar / 2 (foo = (bar / 2)) (= foo (/ bar 2)) lvalue rvalue

17 x = y = z = 4 + 2 (x = (y = (z = (4 + 2)))) (((x = y) = z) = (4 + 2)) the = operator is right-to-left associative

18 http://proglit.com/

19 x = 3; cow = 2 + x; foo(1); rat = bar();

20 free-form syntax x = 3; x = 3 ;

21 foo(); // ignore this /* ignore all this too */ bar();

22 /* apple /* banana orange */ lemon */

23 _foo foo_bar $foo foo$bar

24 var var name; var name = expression; var monkey; var zebra = 3;

25 var jeff; foo(jeff); // OK foo(amanda); // error undefined

26 function function(parameters) {body}

27

28 ! not === equals !== not equals &&and ||or < less than > greater than <= less than or equal >= greater than or equal

29 !true// false !false// true !null// true !0// true !“”// true !undefined// true

30 3 === 3// true 3 === -2// false !true === false// true

31 (3 === 3) || (2 > 4) // true

32 http://proglit.com/

33 if / else if (condition) {body} else if (condition) {body} else {body} while while (condition) {body}

34 if (x === 3) { foo(); } else { bar(); }

35 var i = 0; while (i < 5) { foo(); i = i + 1; }

36 var foo = function() { return bar; }; foo(); // error var bar = 3; foo(); // OK

37 var foo = function() { bar = 6; return bar; var bar; }; foo(); // 6

38 nested functions

39 AC AD B E B AD CB C

40

41

42 http://proglit.com/

43 closure

44

45 objects {properties} object[string]

46 var foo = {}; var bar = {“bill”: 1 + 1, “diana”: 11}; var ack = bar[“bill”]; // 2 bar[“bill”] = 3; foo[“ned”] = 8; ack = foo[“ted”]; // undefined

47 var foo = {}; var bar = {bill: 1 + 1, diana: 11}; var ack = bar.bill; // 2 bar.bill = 3; foo.ned = 8; ack = foo.ted; // undefined

48 methods var foo = {}; foo.bar = function() { this.ack = 3; }; foo.bar();

49 var foo = {}; foo.bar = function(x) { x.ack = 3; }; foo.bar(foo);

50 var foo = {}; foo.bar = function() { this = 3; //error }; foo.bar();

51 var bar = function() { return this; }; bar(); // global object passed to this

52 http://proglit.com/

53 object links AC AD B E B AD CB C

54 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

55 var foo = “hello”; var x = foo.length; // 5 x = foo.charAt(1) ; // “e” x = “avast”.charAt(3) ; // “s”

56 charAt lengthcharAtlengthcharAt

57 var Tom = function(foo, bar) { this.foo = foo; this.bar = bar; }; var jane = (new Tom(2, 3)); var david = new Tom(7, 10);

58 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

59 http://proglit.com/

60 arrays [items] array[index]

61 var foo = []; var bar = [4, “hello”, 33]; var ack = bar[2]; // 33 bar[3] = “orange”;

62 var foo = []; var bar = [4, “hello”, 33]; var ack = bar[“2”]; // 33 bar[“3”] = “orange”;

63 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

64 arguments var foo = function(a, b, c) { // arguments[0] equals a // arguments[1] equals b // arguments[2] equals c … };

65 var foo = function(a, b, c) { … }; foo(1, 7, 11, 2, 8); // OK foo(6, 7); // OK

66 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

67 http://proglit.com/

68 exceptions foo(“hello” * true);

69 exceptions try { foo(“hello” * true); bar(); } catch (ex) { ack(ex); }

70 try { throw “We’re screwed.”; bar(); } catch (ex) { ack(ex); }

71 var foo = function () { … // exception }; foo(); // exception

72 try { foo(); // exception bar(); } catch (ex) { ack(); }

73 var bar = function() { try { foo(); // exception } catch (ex) { ack(); } foo(); // exception }; bar(); // exception

74 http://proglit.com/


Download ppt "the javascript language BY SA."

Similar presentations


Ads by Google