Docsity

Javascript Notes 2018, Lecture notes of Web Programming and Technologies

Partial preview of the text

Download Javascript Notes 2018 and more Lecture notes Web Programming and Technologies in PDF only on Docsity!

JavaScript

Overview

Dynamic and Weak Typing

Java-based syntax

Multi-paradigm support

 imperative and object-oriented

 functional

Object-oriented programming using prototypes

Programming constructs

 Selection statements : if/else, switch

 Looping : while, for, for...in

 Modularization : function, Object

 Built-in Types : String, Number, Boolean, Array, Function, Object

Weak Typing

 '10' == 10 → true or false?  alert( '10' + 10 ) →?  alert( '10' * 10 ) →?  alert( '10' * '10' ) →?  Operations may not be type-safe i.e. type rules not enforced properly  Implicit type conversion  Type rules aren't uniform - unpredictable behavior

Conditionals

if / else

if (marks > 50 ) {

alert('pass');

else {

alert('fail')';

Loops

for loop

var nums='';

for(i= 0 ; i < 10 ; i++){

nums = nums + i;

alert(nums);

Loops

while loop

var nums='';

var i=0;

while( i < 10 ){

nums = nums + i + '\n';

i++;

alert(nums);

Functions

The basic unit of modularization in JavaScript

Functions serve multiple purposes in JavaScript

Can be treated just like a type

Basic Syntax

function name(var1,var2,var3,...){

return;

Functions

Some examples

function product (a,b) {

return a*b;

} // returns product

var product = function (a,b) {

return a*b;

} // same function as above – declared differently

Functions

Scoping rules

All variables declared inside function with var keyword

have function scope – i.e. they are local variables

var a=1; alert(a); // output 1 function scopeTest(){ var a=2; alert(a); } scopeTest(); // output 2 alert(a); // output 1 var a=1; alert(a); // output 1 function scopeTest(){ a=2; alert(a); } scopeTest(); // output 2 alert(a); // output 2

Quiz

Write a function that takes as an input a number

and prints it in reverse order

 (^) e.g. 123 printed as 321 

Write a function that calculates average of

numbers passed as parameter

Write a recursive function to calculate factorial

 (^) e.g. 5! = 120

var item = { title : "HTML Specification", status : "available", isAvailable : function (){ return this.status == "available"; } } alert(item.title); // HTML Specification alert(item.isAvailable()); // true

Objects

var item = new Object(); item.title = "HTML Specification"; item.status = "available"; item.isAvailable = function (){ return this.status == "available"; } alert(item.title); // HTML Specification alert(item.isAvailable()); // true

Objects

Iterating Object Properties

var obj = new Item("HTML Specification"); var output = ''; for(x in obj){ output += x + ": " + obj[x] + ";" ; } alert(output);

Extending Objects

var item1 = new Item("HTML Specification"); alert(item1.title); // HTML Specification alert(item1.isAvailable()); // true item1.issue = function(){ this.status = "issued"; } alert( item1.isAvailable() ); // true item1.issue(); alert( item1.isAvailable() ); // false var item2 = new Item("Javascript Specification"); alert(item2.title); // Javascript Specification alert(item2.isAvailable()); // true alert( item2.isAvailable() ); // true item2.issue(); // Exception alert( item2.isAvailable() ); // true