Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 142 Lecture Notes: JavascriptSlide 1 Simple Javascript Example sum = 0; for (i = 1; i < 10; i++) { sum += i*i; }

Similar presentations


Presentation on theme: "CS 142 Lecture Notes: JavascriptSlide 1 Simple Javascript Example sum = 0; for (i = 1; i < 10; i++) { sum += i*i; }"— Presentation transcript:

1 CS 142 Lecture Notes: JavascriptSlide 1 Simple Javascript Example sum = 0; for (i = 1; i < 10; i++) { sum += i*i; }

2 CS 142 Lecture Notes: JavascriptSlide 2 Arrays x = new Array(); x[3] = 49; y = ["a“, 123, 65];

3 CS 142 Lecture Notes: JavascriptSlide 3 Objects x = new Object(); y = {name: "Alice", age: 23, state: "California"}; x.name = "Bob"; x["age"] = 21;

4 CS 142 Lecture Notes: JavascriptSlide 4 Factorial in Javascript function fac(x) { if (x <= 1) { return 1; } return x*fac(x-1); }

5 CS 142 Lecture Notes: JavascriptSlide 5 Method Example o = new Object(); o.count = 0; o.increment = function(inc) { if (inc == undefined) { inc = 1; } this.count += inc; return this.count; }

6 CS 142 Lecture Notes: JavascriptSlide 6 Functions Can Have Properties function plus1(value) { if (plus1.invocations == undefined) { plus1.invocations = 0; } plus1.invocations++; return value+1; }

7 CS 142 Lecture Notes: JavascriptSlide 7 Constructor function Rectangle(width, height) { this.width = width; this.height = height; } r = new Rectangle(26, 14);

8 CS 142 Lecture Notes: JavascriptSlide 8 Methods (wrong way) function Rectangle(width, height) { this.width = width; this.height = height; this.area = function() { return this.width*this.height; } r = new Rectangle(26, 14); a = r.area();

9 CS 142 Lecture Notes: JavascriptSlide 9 Prototypes function Rectangle(width, height) { this.width = width; this.height = height; } Rectangle.prototype.area = function() { return this.width*this.height; } r = new Rectangle(26, 14); a = r.area();

10 CS 142 Lecture Notes: JavascriptSlide 10 Embedding Javascript... //<![CDATA[ alert("Page is loading"); //]]> Click here.... External Javascript File Inline Code Event Handler

11 CS 142 Lecture Notes: CookiesSlide 11


Download ppt "CS 142 Lecture Notes: JavascriptSlide 1 Simple Javascript Example sum = 0; for (i = 1; i < 10; i++) { sum += i*i; }"

Similar presentations


Ads by Google