SlideShare a Scribd company logo
adapted from slides by Alan Smith and Paweł Dorofiejczyk.
Web Information Systems - 2015
Client Side Programming 1
Javascript Introduction
Just an overview
1
Javascript Introduction
Javascript is
◦ A prototype-based scripting
language
◦ Dynamic
◦ Loosely typed
◦ Multi-Paradigm
▫ Object-Oriented
▫ Functional
▫ Imperative
Javascript Introduction
What’s it Good For?
◦ Web Apps (Client Side)
▫ Provides programmatic access to
certain browser features, your
page's elements, their properties,
and their styles
◦ Server Side
▫ node.js
◦ Databases
▫ MongoDB, CouchDB
Javascript Introduction
Anything Else?
◦ Scripting
▫ OpenOffice
▫ Adobe CS
 Photoshop
 Illustrator
 Dreamweaver
◦ Cross-Platform
▫ Enyo
▫ WinJS
Javascript Basics
How to use it
2
Javascript Basics
Local Variables
Introduce Variables with “var”
◦ var myString = “Hi”;
◦ var myNumber = 1;
◦ var myOtherNumber = 1.1;
◦ var myBoolean = true;
Don’t Create a
Variable Without
var!
This is a Global Variable Declaration.
Don’t do this!
Javascript Basics
Variable Types
typeof
◦ typeof “hi”; // string
Gotchas
◦ typeof []; // object
◦ typeof null; // object
◦ typeof NaN;// number
Javascript Basics
Functions
◦ Are objects
◦ Can be anonymous with reference
stored in a variable
▫ no name necessary
◦ Can create other functions
▫ try not to do this
◦ Are closures
▫ have there own scope
◦ Prototypes
▫ more on this later
Javascript Basics
Function Syntax
function foo() {
console.log(“hello world”);
}
is equivalent to the anonymous
var foo = function() {
console.log(“hello world”);
}
Javascript Basics
Function Args
Primitives
◦ always passed by value
Objects
◦ passed by reference
Javascript Basics
Function Args
var test = function(a, b, obj) {
obj.modified = true;
console.log(arguments);
}
var obj = {};
console.log(obj.modified); // undefined
test(1, 'a', obj);
// { '0': 1, '1': 'a', '2': { modified: true } }
console.log(obj.modified); // true
Javascript Basics
Function Scope
◦ Static (lexical)
◦ Created only by function
◦ Function arguments becomes part of
scope
◦ Child scope have reference to
parent scope (scope chain)
◦ this is not scope (!!!)
Javascript Basics
Function Scope
var accumulator = function(x) {
this.sum = (this.sum || 0) + x;
return this.sum;
}
console.log(accumulator(1)); //1
console.log(accumulator(1)); //2
console.log(accumulator.sum); //undefined
console.log(sum); // 2 !!!
Javascript Basics
Notes on “this”
}
var bar = function(x) {
this.x = x;
return this.x;
}
} ← this refers to the global scope by default
In the browser, “this” refers to window
Javascript Basics
Objects
◦ Dynamic
◦ non-ordered
◦ key-value pairs
◦ Array access or object access
◦ Iterable
◦ Created in runtime
◦ Object literal {}
◦ No privacy control at object level
◦ Prototypal inheritance
◦ Constructor functions
Javascript Basics
A Simple Object
◦ var obj = {};
A Little More
◦ var obj = {
name: “Simple Object”
}
Access via
◦ obj.name
◦ obj[“name”]
Javascript Basics
Special Operators
◦ “+” will also concat two strings
▫ 1 + 2 = 3 (as expected)
▫ “foo”+”bar” = “foobar”
◦ Unary “+” will change type
▫ +”1” = 1
◦ “===” and “!==” should be used
instead of “==” and “!=” as the former
will not attempt type conversion.
Javascript Basics
More Special Operators
◦ “&&” and “||” can be used outside of
conditionals for smaller, though less
readable code.
▫ “&&” will guard against null refs
 return obj && obj.name;
▫ “||” will let you assign values only if
they exist.
 var x = name || obj.name;
The Document Object Model
Javascript in the browser
3
The DOM
In the DOM, everything is a node:
◦ The document itself is a document node
◦ All HTML elements are element nodes
◦ All HTML attributes are attribute nodes
◦ Text inside HTML elements are text nodes
◦ Comments are comment nodes
w3Schools
The DOM
Programmatically access page
elements
Create new elements by:
◦ var paragraph = document.createElement('p');
◦ paragraph.id = 'content';
◦ paragraph.appendChild(document.createText
Node('Hello, world'));
Don’t forget to add the element to the DOM
◦ document.body.appendChild(paragraph);
The DOM
Manipulate them by:
Setting properties
◦ var element =
document.getElementById('content');
element.style.color = 'blue';
Calling methods
◦ var firstNode = document.body.childNodes[0];
document.body.removeChild(firstNode);
The DOM
Understanding the DOM is important…
But it is verbose, tedious, may not
behave as expected across browsers.
This leads to implementation and
maintainability issues.
Javascript Libraries
Usable Javascript in the browser
4
Javascript Libraries
Popular Libraries
◦ jQuery
◦ Underscore
◦ Backbone
◦ Angular
Let’s take a look at the difference...
Javascript
var element =
document.getElementById('content');
element.style.color = 'blue';
var firstNode =
document.body.childNodes[0];
document.body.removeChild(firstNode);
Javascript Libraries
JQuery
var element =$(“#content”);
element.css(“color”, “blue”);
or element.css({color:”blue”})
$(‘body:first-child’).remove();
Javascript Libraries
$ is the global jQuery object
◦ It has its own properties and methods
$ is also a function you can call
◦ $.getJSON('student-list.php');
Relies heavily upon the familiar CSS selector
syntax and the Composite design pattern
◦ $('.urgent').css({ backgroundColor: 'red', color:
'white' });
Javascript Libraries
Many methods are both getters and setters,
depending on the parameter(s), or lack thereof
Many of its functions return a jQuery object as
well, which allows method chaining
◦ $("p.neat").addClass("ohmy").show("slow");
Javascript Libraries
More advanced topics next time...
CREDITS
Special thanks to:
◦ Alan Smith
◦ Paweł Dorofiejczyk
▫ http://www.slideshare.net/PaweD
orofiejczyk/lets-
javascript?qid=35826bbe-211d-
4f50-ad02-29143c7efff2

More Related Content

PPTX
Authentication and Authorization in Asp.Net
PPTX
ASP.NET - Life cycle of asp
PPT
Java Persistence API (JPA) Step By Step
PDF
Spring MVC Framework
PDF
Model View Controller (MVC)
PPTX
Introduction to ASP.Net Viewstate
PPT
Java Servlets
PPTX
HTTP request and response
Authentication and Authorization in Asp.Net
ASP.NET - Life cycle of asp
Java Persistence API (JPA) Step By Step
Spring MVC Framework
Model View Controller (MVC)
Introduction to ASP.Net Viewstate
Java Servlets
HTTP request and response

What's hot (20)

PPT
Jsp ppt
PPTX
Introduction to React JS
PPT
Web Service Presentation
PPT
PDF
C# ASP.NET WEB API APPLICATION DEVELOPMENT
PPTX
ASP.NET Page Life Cycle
PDF
Java Basic Oops Concept
PDF
Introduction to HTML5
PDF
Asp.net state management
PPT
Introduction to JavaScript (1).ppt
PDF
jQuery for beginners
PPT
Web Application Introduction
PPT
Ppt of web development
PPSX
JDBC: java DataBase connectivity
PPT
introduction to web technology
PPTX
Web app presentation
PDF
Android resource
PPTX
Introduction to Web Architecture
Jsp ppt
Introduction to React JS
Web Service Presentation
C# ASP.NET WEB API APPLICATION DEVELOPMENT
ASP.NET Page Life Cycle
Java Basic Oops Concept
Introduction to HTML5
Asp.net state management
Introduction to JavaScript (1).ppt
jQuery for beginners
Web Application Introduction
Ppt of web development
JDBC: java DataBase connectivity
introduction to web technology
Web app presentation
Android resource
Introduction to Web Architecture
Ad

Similar to Lecture 5: Client Side Programming 1 (20)

PDF
Basics of JavaScript
PDF
22519 - Client-Side Scripting Language (CSS) chapter 1 notes .pdf
PDF
PDF
JavaScript Programming
PPTX
JavaScript Fundamentals & JQuery
PDF
JavaScript for Web Analysts
PPT
Java Script Programming on Web Application
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
PPT
Presentation JavaScript Introduction Data Types Variables Control Structure
PPT
JavaScript - An Introduction
PPT
Java Script
PDF
Java Script
PPTX
JavaScript_III.pptx
PPT
JavaScript ppt for introduction of javascripta
PPT
JavaScript Workshop
PPT
Javascript.ppt
PPTX
Intro to Javascript
PPT
lecture 6 javascript event and event handling.ppt
PPT
17javascript.ppt17javascript.ppt17javascript.ppt
Basics of JavaScript
22519 - Client-Side Scripting Language (CSS) chapter 1 notes .pdf
JavaScript Programming
JavaScript Fundamentals & JQuery
JavaScript for Web Analysts
Java Script Programming on Web Application
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
Presentation JavaScript Introduction Data Types Variables Control Structure
JavaScript - An Introduction
Java Script
Java Script
JavaScript_III.pptx
JavaScript ppt for introduction of javascripta
JavaScript Workshop
Javascript.ppt
Intro to Javascript
lecture 6 javascript event and event handling.ppt
17javascript.ppt17javascript.ppt17javascript.ppt
Ad

Recently uploaded (20)

PPTX
master seminar digital applications in india
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial disease of the cardiovascular and lymphatic systems
2.FourierTransform-ShortQuestionswithAnswers.pdf
Basic Mud Logging Guide for educational purpose
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
TR - Agricultural Crops Production NC III.pdf
O7-L3 Supply Chain Operations - ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pre independence Education in Inndia.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Renaissance Architecture: A Journey from Faith to Humanism
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
VCE English Exam - Section C Student Revision Booklet
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 4: Burden of Disease Tutorial Slides S2 2025

Lecture 5: Client Side Programming 1

  • 1. adapted from slides by Alan Smith and Paweł Dorofiejczyk. Web Information Systems - 2015 Client Side Programming 1
  • 3. Javascript Introduction Javascript is ◦ A prototype-based scripting language ◦ Dynamic ◦ Loosely typed ◦ Multi-Paradigm ▫ Object-Oriented ▫ Functional ▫ Imperative
  • 4. Javascript Introduction What’s it Good For? ◦ Web Apps (Client Side) ▫ Provides programmatic access to certain browser features, your page's elements, their properties, and their styles ◦ Server Side ▫ node.js ◦ Databases ▫ MongoDB, CouchDB
  • 5. Javascript Introduction Anything Else? ◦ Scripting ▫ OpenOffice ▫ Adobe CS  Photoshop  Illustrator  Dreamweaver ◦ Cross-Platform ▫ Enyo ▫ WinJS
  • 7. Javascript Basics Local Variables Introduce Variables with “var” ◦ var myString = “Hi”; ◦ var myNumber = 1; ◦ var myOtherNumber = 1.1; ◦ var myBoolean = true;
  • 8. Don’t Create a Variable Without var! This is a Global Variable Declaration. Don’t do this!
  • 9. Javascript Basics Variable Types typeof ◦ typeof “hi”; // string Gotchas ◦ typeof []; // object ◦ typeof null; // object ◦ typeof NaN;// number
  • 10. Javascript Basics Functions ◦ Are objects ◦ Can be anonymous with reference stored in a variable ▫ no name necessary ◦ Can create other functions ▫ try not to do this ◦ Are closures ▫ have there own scope ◦ Prototypes ▫ more on this later
  • 11. Javascript Basics Function Syntax function foo() { console.log(“hello world”); } is equivalent to the anonymous var foo = function() { console.log(“hello world”); }
  • 12. Javascript Basics Function Args Primitives ◦ always passed by value Objects ◦ passed by reference
  • 13. Javascript Basics Function Args var test = function(a, b, obj) { obj.modified = true; console.log(arguments); } var obj = {}; console.log(obj.modified); // undefined test(1, 'a', obj); // { '0': 1, '1': 'a', '2': { modified: true } } console.log(obj.modified); // true
  • 14. Javascript Basics Function Scope ◦ Static (lexical) ◦ Created only by function ◦ Function arguments becomes part of scope ◦ Child scope have reference to parent scope (scope chain) ◦ this is not scope (!!!)
  • 15. Javascript Basics Function Scope var accumulator = function(x) { this.sum = (this.sum || 0) + x; return this.sum; } console.log(accumulator(1)); //1 console.log(accumulator(1)); //2 console.log(accumulator.sum); //undefined console.log(sum); // 2 !!!
  • 16. Javascript Basics Notes on “this” } var bar = function(x) { this.x = x; return this.x; } } ← this refers to the global scope by default In the browser, “this” refers to window
  • 17. Javascript Basics Objects ◦ Dynamic ◦ non-ordered ◦ key-value pairs ◦ Array access or object access ◦ Iterable ◦ Created in runtime ◦ Object literal {} ◦ No privacy control at object level ◦ Prototypal inheritance ◦ Constructor functions
  • 18. Javascript Basics A Simple Object ◦ var obj = {}; A Little More ◦ var obj = { name: “Simple Object” } Access via ◦ obj.name ◦ obj[“name”]
  • 19. Javascript Basics Special Operators ◦ “+” will also concat two strings ▫ 1 + 2 = 3 (as expected) ▫ “foo”+”bar” = “foobar” ◦ Unary “+” will change type ▫ +”1” = 1 ◦ “===” and “!==” should be used instead of “==” and “!=” as the former will not attempt type conversion.
  • 20. Javascript Basics More Special Operators ◦ “&&” and “||” can be used outside of conditionals for smaller, though less readable code. ▫ “&&” will guard against null refs  return obj && obj.name; ▫ “||” will let you assign values only if they exist.  var x = name || obj.name;
  • 21. The Document Object Model Javascript in the browser 3
  • 22. The DOM In the DOM, everything is a node: ◦ The document itself is a document node ◦ All HTML elements are element nodes ◦ All HTML attributes are attribute nodes ◦ Text inside HTML elements are text nodes ◦ Comments are comment nodes w3Schools
  • 23. The DOM Programmatically access page elements Create new elements by: ◦ var paragraph = document.createElement('p'); ◦ paragraph.id = 'content'; ◦ paragraph.appendChild(document.createText Node('Hello, world')); Don’t forget to add the element to the DOM ◦ document.body.appendChild(paragraph);
  • 24. The DOM Manipulate them by: Setting properties ◦ var element = document.getElementById('content'); element.style.color = 'blue'; Calling methods ◦ var firstNode = document.body.childNodes[0]; document.body.removeChild(firstNode);
  • 25. The DOM Understanding the DOM is important… But it is verbose, tedious, may not behave as expected across browsers. This leads to implementation and maintainability issues.
  • 27. Javascript Libraries Popular Libraries ◦ jQuery ◦ Underscore ◦ Backbone ◦ Angular Let’s take a look at the difference...
  • 28. Javascript var element = document.getElementById('content'); element.style.color = 'blue'; var firstNode = document.body.childNodes[0]; document.body.removeChild(firstNode); Javascript Libraries JQuery var element =$(“#content”); element.css(“color”, “blue”); or element.css({color:”blue”}) $(‘body:first-child’).remove();
  • 29. Javascript Libraries $ is the global jQuery object ◦ It has its own properties and methods $ is also a function you can call ◦ $.getJSON('student-list.php'); Relies heavily upon the familiar CSS selector syntax and the Composite design pattern ◦ $('.urgent').css({ backgroundColor: 'red', color: 'white' });
  • 30. Javascript Libraries Many methods are both getters and setters, depending on the parameter(s), or lack thereof Many of its functions return a jQuery object as well, which allows method chaining ◦ $("p.neat").addClass("ohmy").show("slow");
  • 31. Javascript Libraries More advanced topics next time...
  • 32. CREDITS Special thanks to: ◦ Alan Smith ◦ Paweł Dorofiejczyk ▫ http://www.slideshare.net/PaweD orofiejczyk/lets- javascript?qid=35826bbe-211d- 4f50-ad02-29143c7efff2