38
Introduction to Javascript Web Development 101 Lesson 02.01

02 Introduction to Javascript

  • Upload
    crgwbr

  • View
    426

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 02 Introduction to Javascript

Introduction to JavascriptWeb Development 101 Lesson 02.01

Page 2: 02 Introduction to Javascript

Java·script NounAn interpreted computer programming language. As part of web browsers, implementations allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content that is displayed.

Page 3: 02 Introduction to Javascript

DISCUSSWHY AND HOW DOES JAVASCRIPT ENABLE GMAIL TO WORK?

Page 4: 02 Introduction to Javascript

Example 02.01.01 http://jsfiddle.net/crgwbr/Fqhy4/

Page 5: 02 Introduction to Javascript

var writeTime = function () { var parent = document.getElementById('clock'), timeElem = document.createElement('p'), time = new Date();! timeElem.innerHTML = time.toUTCString(); parent.appendChild(timeElem);};!writeTime();

Javascript clock

Page 6: 02 Introduction to Javascript

var writeTime = function () { var parent = document.getElementById('clock'), timeElem = document.createElement('p'), time = new Date();! timeElem.innerHTML = time.toUTCString(); parent.innerHTML = “”; parent.appendChild(timeElem);};!setInterval(writeTime, 500);

Javascript clock

Page 7: 02 Introduction to Javascript

var writeTime = function () { var parent = document.getElementById('clock'), timeElem = document.createElement('p'), time = new Date();! timeElem.innerHTML = time.toUTCString(); parent.innerHTML = “”; parent.appendChild(timeElem);};!setInterval(writeTime, 500);

The DOM API

Page 8: 02 Introduction to Javascript

Javascript [proper]

Page 9: 02 Introduction to Javascript

Data TypesString: var myString = “Hello World”;!Number var myNum = 42;!Array var myArr = [5, 6, “seven”, 8];!Object var myObj = { key1: “Value”, key2: 42, anotherKey: myArr };

Page 10: 02 Introduction to Javascript

Statements & ExpressionsAn expression produces a value.

Can be written wherever a value is expected.

A value is in itself an expression.

A statement performs an action.

Statements manipulate interpreter flow and perform actions.

Wherever a statement is expected, you may write an Expression. The opposite is not true.

Page 11: 02 Introduction to Javascript

Expressions

var f = function(x) { return (x * x) + 5; };!var a = f(1);var b = f(2 * 3);

f(x) → x2 + 5

!f(1) ≍ 1 f(2 × 3) ≍ 36

Page 12: 02 Introduction to Javascript

Statements

var myStr = “Hello World”, i;!for (i = 0; i < myStr.length; i++) { console.log(myStr[i]);}

var myStr = “Hello World”, i = 0;!while (i < myStr.length) { i++; console.log(myStr[i]);}

Page 13: 02 Introduction to Javascript

Statementsvar comp = function(x, y) { if (x > y) { return 1; } else if (x < y) { return -1; }! return 0;};

var comp = function(x, y) { if (x > y) { return 1; } else { if (x < y) { return -1; } }! return 0;};

Page 14: 02 Introduction to Javascript

Getting Fancy

var random = (function() { return 4; // Verified random by dice roll}());

Page 15: 02 Introduction to Javascript

IIFE Immediately Invoked Function Expression.

Why?

Page 16: 02 Introduction to Javascript

scope The context within the program in which an identifier is valid and can be resolved to find the entity associated with the identifier.

Page 17: 02 Introduction to Javascript

Javascript has lexical scoping nested at the function level, with the global scope being the outermost scope.

Page 18: 02 Introduction to Javascript

Global Scope is dangerousmyCoolApp.js!var myFunc = function() { ...};

someFancyPlugin.js!var myFunc = function() { ...};

Page 19: 02 Introduction to Javascript

Something more sanemyCoolApp.js!(function() { var myFunc = function() { ... };}());

someFancyPlugin.js!(function() { var myFunc = function() { ... };}());

Page 20: 02 Introduction to Javascript

Closure A function together with a referencing environment—a table storing a reference to each of the non-local variables of that function. A closure allows a function to access non-local variables even when invoked outside its immediate lexical scope.

Page 21: 02 Introduction to Javascript

Closuresvar increment = (function() { var i = 0, inc; inc = function(step) { i += step; };! return inc;}());

>>> increment(1);1>>> increment(1);2>>> increment(5);7>>> iundefined

Page 22: 02 Introduction to Javascript

Closuresvar counter = function(step) { var i = 0, inc;! inc = function() { i += step; };! return inc;};

>>> var n = counter(5);>>> n();5>>> n();10>>> n();15

Page 23: 02 Introduction to Javascript

Brainstorm I need to read the state of the counter without incrementing it. How?

Page 24: 02 Introduction to Javascript

OOJSvar Counter = function(step) { var i = 0;! this.inc = function() { i += step; };! this.get = function() { return i; }};

>>> var n = new Counter(5);>>> n.get();0>>> n.inc();5>>> n.get();5

Page 25: 02 Introduction to Javascript

OOJSvar Counter = function(step) { this.i = 0; this.step = step;};!Counter.prototype = { inc: function() { this.i += this.step; },! get: function() { return this.i; }};

>>> var n = new Counter(5);>>> n.get();0>>> n.inc();5>>> n.get();5

Page 26: 02 Introduction to Javascript

Javascript [DOM]

Page 27: 02 Introduction to Javascript

RequirementsWe just wrote a counter object

Add a user interface

Current value should be displayed in the browser document

There should be a button to increment the count

There should be another button to reset the count

Page 28: 02 Introduction to Javascript

Example 02.01.02 http://jsfiddle.net/crgwbr/ynraf/

Page 29: 02 Introduction to Javascript

http://jsfiddle.net/crgwbr/ynraf/

Page 30: 02 Introduction to Javascript

Requirements Change

Page now needs to have support having n number of counters

Defaults to 3 counters

User can add another by clicking a button

Page 31: 02 Introduction to Javascript

Example 02.01.03 http://jsfiddle.net/crgwbr/UjF5n/

Page 32: 02 Introduction to Javascript

http://jsfiddle.net/crgwbr/UjF5n/

Page 33: 02 Introduction to Javascript

Requirements Change

User should be able to delete any counter on the page

User can name counters they add

Page 34: 02 Introduction to Javascript

Example 02.01.04 http://jsfiddle.net/crgwbr/ejnN2/

Page 35: 02 Introduction to Javascript

http://jsfiddle.net/crgwbr/ejnN2/

Page 36: 02 Introduction to Javascript

That’s a Web Application.

Page 37: 02 Introduction to Javascript

ReviewJavascript is a general purpose, interpreted language.

First-class functions

C-style syntax

Prototypical Inheritance

Runs in a Virtual Machine

Access the outside world through injected APIs

DOM is an API for interacting with the browser and it’s HTML document

Most UI code is event driven

When used well, closures are awesome

Page 38: 02 Introduction to Javascript

HomeworkRead: Web Fundamental—Javascript

Watch: Javascript—The Good Parts

http://www.youtube.com/watch?v=hQVTIJBZook