Professional Documents
Culture Documents
Distributed Systems
Distributed Systems
Third part*
* This presentation was created using the content of Code School courses and
http://www.w3schools.com
Agenda
ü Data/document models and languages
2
Fundamental concepts
n Objects, values, and variables
n Function declarations
3
Objects and values
n In JavaScript, almost "everything" is an object:
¨ Booleans can be objects (or primitive data treated as objects)
¨ Numbers can be objects (or primitive data treated as objects)
¨ Strings can be objects (or primitive data treated as objects)
¨ Dates are always objects
¨ Maths are always objects
¨ Regular expressions are always objects
¨ Arrays are always objects
¨ Functions are always objects
¨ Objects are objects
4
String values (1)
n Concatenation operation with:
Ø Spaces: "The meaning of life is " + 42
Ø Expressions: "Platform " + 9 + " and " + 3/4
5
String values (2)
n Length property:
Ø "numeral palindrome".length is equal to 18
n Comparisons:
Ø "The Wright Brothers" == "The Wright Brothers" is true
Ø "The Wright Brothers" == "Super Mario Brothers" is false
Ø "The Wright Brothers" != "The Wright brothers" is true
6
Variables
n JavaScript variables can contain single values:
Ø var person = "John Doe";
7
Variables names with strings
n Variable names can also access the length property:
var longString = "I would not want to retype this String every time.";
longString.length;
8
Variables scope
n Local variables:
¨ Variables declared within a function, become LOCAL to the function
¨ Local variables have local scope: They can only be accessed within the
function
n Global variables:
¨ A variable declared outside a function, becomes GLOBAL
¨ A global variable has global scope: All scripts and functions on a web
page can access it
9
Fundamental concepts
ü Objects, values, and variables
n Function declarations
10
Comparison operators (i=1)
Operator Condition Example
== equal to i == "1"
!= not equal i != 1
11
Logical operators
Operator Operation Examples
12
Conditional instructions
n Ternary operator syntax:
variablename = (condition) ? value1:value2;
13
Loop While: Syntax
while( boolean_expression ) {
code_block;
}
do {
code_block;
} while( boolean_expression );
14
Loop For: Syntax
for( initialize[,initialize];
boolean_expression; update[,update] )
{
code_block;
}
15
For/In Loop: Example
var person = { firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue" };
16
Fundamental concepts
ü Objects, values, and variables
n Function declarations
17
Get and send information
n prompt() sends a message and retrieves an entry from the user:
prompt("What is your name?")
Ø The typeof operator is useful for identifying the type of value inside
a variable or expression
18
Build-in functions: Example
var gotName = false;
while(gotName == false) {
var userName = prompt("What is your name?");
19
Fundamental concepts
ü Objects, values, and variables
n Function declarations
20
JavaScript in an HTML file (1)
n JavaScript can be placed in the <body> and the <head> sections of
an HTML page:
¨ JavaScript code must be inserted between <script> and </script> tags
¨ Or embedding it signaling which JavaScript file to use:
<!DOCTYPE html>
<html>
<head>
<script src="trains.js"></script>
</head>
<body>
<h1>JAVASCRIPT EXPRESS!</h1>
…
</body>
</html>
21
JavaScript in an HTML file (2)
n JavaScript does NOT have any built-in print or display functions:
¨ Writing into an alert box, using window.alert()
¨ Writing into the HTML output using document.write()
¨ Writing into an HTML element, using innerHTML
¨ Writing into the browser console, using console.log()
22
Ø Supervised work
Fundamental concepts
ü Objects, values, and variables
n Function declarations
25
JS functions (1)
n Function declaration syntax:
function functionName(parameters) {
code to be executed
}
26
JS functions (2)
n Functions are objects:
¨ The typeof operator in JavaScript returns "function" for functions
¨ JavaScript functions have both properties and methods
¨ The arguments.length property returns the number of arguments
received when the function was invoked:
function myFunction() {
return arguments.length;
}
27
JS function example
n Function that counts "E's" in a user-entered phrase:
function countE() {
var phrase = prompt("Which phrase would you like to examine?");
if(typeof(phrase) != "string") {
alert("That's not a valid entry!");
return false;
} else {
var eCount = 0;
for(var i = 0; i < phrase.length; i++) {
if(phrase.charAt(i) == 'e' || phrase.charAt(i) == 'E') {
eCount++;
}
}
alert("There are " + eCount + " E's in \"" + phrase + "\".");
return true;
}
}
28
Fundamental concepts
ü Objects, values, and variables
ü Function declarations
29
JS arrays (1)
n Arrays are a special type of objects:
Ø typeof returns "object" for arrays
Ø They use numbers to access its "elements"
n Creating an array:
Ø var array-name = [item1, item2, ...];
Ø var array-name = new Array( item1, item2, ... );
30
JS arrays (2)
n The length property of an array returns the length of an array (the
number of array elements)
31
Building a passenger list
n Using an array and functions to keep track of train passengers:
function addPassenger( name, list ) {
if(list.length == 0) {
list.push(name);
} else {
for(var i = 0; i < list.length; i++) {
if(list[i] == undefined) {
list[i] = name;
} else if(i == list.length - 1) {
list.push(name);
}
}
}
return list;
}
32
Removing passengers
n Using an array and functions to keep track of train passengers:
function deletePassenger( name, list ) {
if(list.length == 0) {
console.log("List is empty!");
} else {
for(var i = 0; i < list.length; i++) {
if(list[i] == name) {
list[i] = undefined;
return list;
} else if(i == list.length - 1) {
console.log("Passenger not found!");
}
}
}
return list;
}
33
Ø Supervised work
Fundamental concepts
ü Objects, values, and variables
ü Function declarations
36
Inheritance
Clothing
- id: String
- description: String
- price: String
+ displayInformation( )
37
Anonymous closures
var CLOTHING = (function( d, p ) {
CLOTHING.counter = (++CLOTHING.counter || 1);
var id = CLOTHING.counter;
var description = d;
var price = p;
return {
getID: function() { return id; },
getDescription: function() { return description; },
getPrice: function() { return price; },
displayInformation: function() {
console.log('Clothing id: ' + id);
console.log('Clothing description: ' + description);
console.log('Clothing price: ' + price);
}
}
});
38
Augmentation (1)
var SHIRT = (function( d, p, s, c ) {
var shirt = CLOTHING(d, p);
var size = (s == 'S' || s == 'M' || s == 'L') ? s : 'U';
var colorCode = (c == 'R' || c == 'G' || c == 'B') ? c : 'U';
shirt.getSize = function() { return size; };
shirt.getColorCode = function() { return colorCode; };
shirt.displayInformation = function() {
console.log('Shirt id: ' + shirt.getID());
console.log('Shirt description: ' + shirt.getDescription());
console.log('Shirt price: ' + shirt.getPrice());
console.log('Shirt size: ' + size);
console.log('Shirt color: ' + colorCode);
};
return shirt;
});
39
Augmentation (2)
var HAT = (function( d, p, c ) {
var hat = CLOTHING(d, p);
var colorCode = (c == 'R' || c == 'G' || c == 'B') ? c : 'U';
hat.getColorCode = function() { return colorCode; };
hat.displayInformation = function() {
console.log('Hat id: ' + hat.getID());
console.log('Hat description: ' + hat.getDescription());
console.log('Hat price: ' + hat.getPrice());
console.log('Hat color: ' + colorCode);
};
return hat;
});
var shirt = SHIRT( 'T-Shirt', 19.99, 'M', 'R' );
40
Agenda
ü Data/document models and languages
41
HTML local storage
n Web applications can store data locally within the user's browser:
¨ Before HTML5, application data had to be stored in cookies, included in every
server request
¨ Unlike cookies, the storage limit is far larger (at least 5MB) and information is
never transferred to the server
¨ Local storage is more secure, and large amounts of data can be stored locally,
without affecting website performance
n HTML local storage provides two objects for storing data on the
client*:
¨ window.localStorage: It stores data with no expiration date
¨ window.sessionStorage: It stores data for one session (data is lost when the
browser tab is closed
42