SlideShare a Scribd company logo
Client Side Programming 2
Web Information Systems - 2015
Advanced Javascript
Let’s take this a step further
1
Advanced Javascript
Function Prototypes
● Every JavaScript object has a
prototype.
● The prototype is also an object.
● All JavaScript objects inherit their
properties and methods from their
prototype.
src: w3schools
Advanced Javascript
Object Constructor
function person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}
var me = new person(“jeremy”, “Brunn”, 34)
src: w3schools
Advanced Javascript
Add a property
● me.favoriteFood = “pizza”
Add a property to a prototype
● person.favoriteFood = “”
Add a method
● me.getName = function() {
return firstName+”“+lastName;
}
src: w3schools
Advanced Javascript
Adding to constructors
function person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.getName = function() {
return firstName+”
“+lastName;
}
src: w3schools
Advanced Javascript
Adding via prototype
person.prototype.getName = function() {
return this.firstName+” “+this.lastName;
}
person.prototype.setFaveFood =
function(food) {
this.favoriteFood = food;
} src: w3schools
Advanced Javascript
Ajax and Callbacks
Asynchronous Javascript and XML
● data to/from the server side
$.ajax({
url: “http://www.someServer.com”
}).done(success_callback)
.fail(fail_callback)
.always(always_callback)
Advanced Javascript
Callbacks
● functions passed to other functions as
arguments.
In the previous example, success_callback
references a function that will be called when
the ajax request completes successfully;
otherwise, the function referenced by
fail_callback will be called. No matter what,
always_callback will be called.
Advanced Javascript
Special Considerations
When requesting data from another domain,
it will probably be necessary to add {
dataType: “jsonp” } to the ajax argument
object.
● Same-origin Policy
● Cross-origin resource sharing
Also, you can’t make an ajax call to your
local computer's file system, you’ll need a
local server running to test.
Advanced Javascript
Javascript Aids
These help clean code and suggest
corrections to problems before they arise.
● JSLint
● JSHint - I prefer this one. It’s not quite so
restrictive.
src: w3schools
Advanced Libraries
Doing more with less
2
Javascript Libraries
Previously, we looked at accessing
elements on the DOM with JQuery.
Now we’ll examine some other things
that we can do with Javascript through
JQuery and other libraries.
Javascript Libraries
DOM Event listeners in JQuery
$(“#submit”).click(function(e){
// do something
})
is shorthand for
$(document).on.(“click”, “#submit”,
function(e){
// do something
})
Javascript Libraries
DOM Event listeners in JQuery
$(“#submit”).click(function(e){
// e is the event to handle
e.preventDefault();
e.stopPropagation();
targetElement = e.currentTarget;
var id = targetElement.id;
// id = “submit”
})
Javascript Libraries
Common Events
◦ click - an element is clicked
◦ dblclick - an element is dbl clicked
◦ focus - an element gets focus
◦ change - an element’s value changes
▫ <input>, <textarea>, or <select>
◦ hover - an element is hovered over
◦ keydown - user presses a key on
keyboard
◦ keyup - user releases a key on the
keyboard
Javascript Libraries
Programmatically Triggering Events
$(“#submit”).trigger(“click”);
Javascript Libraries
Iterating elements in JQuery
var liArray = [];
$(“ul#myList > li”).each( function(i) {
liArray.push( $(this).text() );
// add the li text (string) to the array
// this is the current element
// i is the index
});
Javascript Libraries
Iterating in JQuery (and appending el)
var numArray = [ 1, 2, 3, 4, 5 ];
$.each(numArray, function(index, value) {
$(“#myList”).append($(“<li>”).text(value));
// adds lis to the ul “#myList”
});
Javascript Libraries
Underscore
An awesome library for accessing data,
and much more...
instead of JQuery’s “$”, use “_”
First, a comparison.
Javascript Libraries
Iterating lists in underscore
var numArray = [ 1, 2, 3, 4 ];
_.each(numArray, function(value, index) {
$(“#myUL”).append($(“<li>”).text(value));
// adds lis to the ul “#myUL”
});
Notice “value” and “index” are reversed
Also, you cannot break from _.each()
Javascript Libraries
Iterating objects in undescore
var numObj = { a: 1, b: 2, c: 3, d: 4 };
_.each(numObj, function(value, key) {
$(“#myUL”).append(
$(“<li>”).text(key+” : “+value));
// adds lis to the ul “#myUL”
});
Javascript Libraries
Some more examples using this list of
people objects.
var peopleList = [
{ name: ”Jeremy”, role: “student” },
{ name: ”Tanvi”, role: “instructor”
},
{ name: ”Pavan”, role: “student” },
{ name: “Pramod”, role: “student” }
];
Javascript Libraries
_.pluck(numObj, “name”);
// [“Jeremy”, ”Tanvi”, “Pavan”, “Pramod”]
_.where(numObj, { role: “instructor”});
// [{ name: “Tanvi”, role: “instructor” }]
_.groupBy(peopleList, {role: "student"})
// { false: [ { name: "Tanvi", role: "instructor" } ],
true: [ { name: "Jeremy", role: "student" },
{ name: "Pavan", role: "student" },
{ name: "Pramod", role: "student" }
]}
Javascript Libraries
Underscore also provides helper
functions.
_.isNaN()
_.isNull()
_.isEmpty()
_.isFunction()
_.isUndefined()
And more
Javascript Frameworks
Production Ready Code
3
Javascript Frameworks
Javascript is a great language, but it’s
very open, often misunderstood, and
ease to write poorly.
Using a framework can help you
conform to some common
programming practices, like MVC.
Javascript Frameworks
Backbone.js is a great framework that
provides Models, Views, and
Collections that abstract abstracts a lot
of the common functionality of a web
app.
◦ ajax calls
◦ event bindings
◦ model changes
TODO example
All Done

More Related Content

PPTX
Lecture 5: Client Side Programming 1
Artificial Intelligence Institute at UofSC
 
PPT
jQuery
Mohammed Arif
 
PPTX
JavaScript and jQuery Basics
Kaloyan Kosev
 
KEY
Week3
Will Gaybrick
 
PPTX
Object Oriented Programming In JavaScript
Forziatech
 
PPT
Java script
vishal choudhary
 
PPTX
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
PDF
jQuery -Chapter 2 - Selectors and Events
WebStackAcademy
 
Lecture 5: Client Side Programming 1
Artificial Intelligence Institute at UofSC
 
JavaScript and jQuery Basics
Kaloyan Kosev
 
Object Oriented Programming In JavaScript
Forziatech
 
Java script
vishal choudhary
 
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
jQuery -Chapter 2 - Selectors and Events
WebStackAcademy
 

What's hot (20)

PPT
OOP in JavaScript
manugoel2003
 
PDF
Java script tutorial
Doeun KOCH
 
PPTX
Java script
Sukrit Gupta
 
PPT
JavaScript & Dom Manipulation
Mohammed Arif
 
PPT
JQuery introduction
NexThoughts Technologies
 
PPTX
jQuery PPT
Dominic Arrojado
 
PPTX
jQuery
Dileep Mishra
 
PPTX
jQuery
Jay Poojara
 
PPTX
jQuery
Vishwa Mohan
 
PDF
jQuery - Chapter 5 - Ajax
WebStackAcademy
 
PPTX
Object Oriented JavaScript - II
TO THE NEW | Technology
 
PPT
JavaScript: Ajax & DOM Manipulation
borkweb
 
PPTX
jQuery Presentation
Rod Johnson
 
PPT
Learn javascript easy steps
prince Loffar
 
PDF
jQuery Introduction
Arwid Bancewicz
 
PDF
Introduction to jQuery
Zeeshan Khan
 
PPT
J Query
ravinxg
 
PDF
Advanced javascript
Doeun KOCH
 
PPTX
Tips for writing Javascript for Drupal
Sergey Semashko
 
OOP in JavaScript
manugoel2003
 
Java script tutorial
Doeun KOCH
 
Java script
Sukrit Gupta
 
JavaScript & Dom Manipulation
Mohammed Arif
 
JQuery introduction
NexThoughts Technologies
 
jQuery PPT
Dominic Arrojado
 
jQuery
Jay Poojara
 
jQuery
Vishwa Mohan
 
jQuery - Chapter 5 - Ajax
WebStackAcademy
 
Object Oriented JavaScript - II
TO THE NEW | Technology
 
JavaScript: Ajax & DOM Manipulation
borkweb
 
jQuery Presentation
Rod Johnson
 
Learn javascript easy steps
prince Loffar
 
jQuery Introduction
Arwid Bancewicz
 
Introduction to jQuery
Zeeshan Khan
 
J Query
ravinxg
 
Advanced javascript
Doeun KOCH
 
Tips for writing Javascript for Drupal
Sergey Semashko
 
Ad

Similar to Lecture 6: Client Side Programming 2 (20)

PPTX
Jquery plugin development
Faruk Hossen
 
PDF
Learning jQuery made exciting in an interactive session by one of our team me...
Thinqloud
 
ODP
Jquery- One slide completing all JQuery
Knoldus Inc.
 
PDF
jQuery
Ivano Malavolta
 
PPTX
IWT presentation121232112122222225556+556.pptx
dgfs55437
 
PPTX
JavaScript- Functions and arrays.pptx
Megha V
 
PPTX
UNIT 1 (7).pptx
DrDhivyaaCRAssistant
 
PPTX
J query resh
Resh Mahtani
 
KEY
jQuery: Tips, tricks and hints for better development and Performance
Jonas De Smet
 
PDF
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
Guy Royse
 
PPTX
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Jon Kruger
 
PDF
Introducing jQuery
Wildan Maulana
 
PPTX
Intro to Javascript
Anjan Banda
 
PDF
Understanding backbonejs
Nick Lee
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PDF
The curious Life of JavaScript - Talk at SI-SE 2015
jbandi
 
PPTX
Jquery beltranhomewrok
Catherine Beltran
 
PPTX
Jquery beltranhomewrok
Catherine Beltran
 
PPT
Jquery Best Practices
brinsknaps
 
Jquery plugin development
Faruk Hossen
 
Learning jQuery made exciting in an interactive session by one of our team me...
Thinqloud
 
Jquery- One slide completing all JQuery
Knoldus Inc.
 
IWT presentation121232112122222225556+556.pptx
dgfs55437
 
JavaScript- Functions and arrays.pptx
Megha V
 
UNIT 1 (7).pptx
DrDhivyaaCRAssistant
 
J query resh
Resh Mahtani
 
jQuery: Tips, tricks and hints for better development and Performance
Jonas De Smet
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
Guy Royse
 
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Jon Kruger
 
Introducing jQuery
Wildan Maulana
 
Intro to Javascript
Anjan Banda
 
Understanding backbonejs
Nick Lee
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
The curious Life of JavaScript - Talk at SI-SE 2015
jbandi
 
Jquery beltranhomewrok
Catherine Beltran
 
Jquery beltranhomewrok
Catherine Beltran
 
Jquery Best Practices
brinsknaps
 
Ad

Recently uploaded (20)

DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 

Lecture 6: Client Side Programming 2

  • 1. Client Side Programming 2 Web Information Systems - 2015
  • 2. Advanced Javascript Let’s take this a step further 1
  • 3. Advanced Javascript Function Prototypes ● Every JavaScript object has a prototype. ● The prototype is also an object. ● All JavaScript objects inherit their properties and methods from their prototype. src: w3schools
  • 4. Advanced Javascript Object Constructor function person(first, last, age) { this.firstName = first; this.lastName = last; this.age = age; } var me = new person(“jeremy”, “Brunn”, 34) src: w3schools
  • 5. Advanced Javascript Add a property ● me.favoriteFood = “pizza” Add a property to a prototype ● person.favoriteFood = “” Add a method ● me.getName = function() { return firstName+”“+lastName; } src: w3schools
  • 6. Advanced Javascript Adding to constructors function person(first, last, age) { this.firstName = first; this.lastName = last; this.age = age; this.getName = function() { return firstName+” “+lastName; } src: w3schools
  • 7. Advanced Javascript Adding via prototype person.prototype.getName = function() { return this.firstName+” “+this.lastName; } person.prototype.setFaveFood = function(food) { this.favoriteFood = food; } src: w3schools
  • 8. Advanced Javascript Ajax and Callbacks Asynchronous Javascript and XML ● data to/from the server side $.ajax({ url: “http://www.someServer.com” }).done(success_callback) .fail(fail_callback) .always(always_callback)
  • 9. Advanced Javascript Callbacks ● functions passed to other functions as arguments. In the previous example, success_callback references a function that will be called when the ajax request completes successfully; otherwise, the function referenced by fail_callback will be called. No matter what, always_callback will be called.
  • 10. Advanced Javascript Special Considerations When requesting data from another domain, it will probably be necessary to add { dataType: “jsonp” } to the ajax argument object. ● Same-origin Policy ● Cross-origin resource sharing Also, you can’t make an ajax call to your local computer's file system, you’ll need a local server running to test.
  • 11. Advanced Javascript Javascript Aids These help clean code and suggest corrections to problems before they arise. ● JSLint ● JSHint - I prefer this one. It’s not quite so restrictive. src: w3schools
  • 13. Javascript Libraries Previously, we looked at accessing elements on the DOM with JQuery. Now we’ll examine some other things that we can do with Javascript through JQuery and other libraries.
  • 14. Javascript Libraries DOM Event listeners in JQuery $(“#submit”).click(function(e){ // do something }) is shorthand for $(document).on.(“click”, “#submit”, function(e){ // do something })
  • 15. Javascript Libraries DOM Event listeners in JQuery $(“#submit”).click(function(e){ // e is the event to handle e.preventDefault(); e.stopPropagation(); targetElement = e.currentTarget; var id = targetElement.id; // id = “submit” })
  • 16. Javascript Libraries Common Events ◦ click - an element is clicked ◦ dblclick - an element is dbl clicked ◦ focus - an element gets focus ◦ change - an element’s value changes ▫ <input>, <textarea>, or <select> ◦ hover - an element is hovered over ◦ keydown - user presses a key on keyboard ◦ keyup - user releases a key on the keyboard
  • 17. Javascript Libraries Programmatically Triggering Events $(“#submit”).trigger(“click”);
  • 18. Javascript Libraries Iterating elements in JQuery var liArray = []; $(“ul#myList > li”).each( function(i) { liArray.push( $(this).text() ); // add the li text (string) to the array // this is the current element // i is the index });
  • 19. Javascript Libraries Iterating in JQuery (and appending el) var numArray = [ 1, 2, 3, 4, 5 ]; $.each(numArray, function(index, value) { $(“#myList”).append($(“<li>”).text(value)); // adds lis to the ul “#myList” });
  • 20. Javascript Libraries Underscore An awesome library for accessing data, and much more... instead of JQuery’s “$”, use “_” First, a comparison.
  • 21. Javascript Libraries Iterating lists in underscore var numArray = [ 1, 2, 3, 4 ]; _.each(numArray, function(value, index) { $(“#myUL”).append($(“<li>”).text(value)); // adds lis to the ul “#myUL” }); Notice “value” and “index” are reversed Also, you cannot break from _.each()
  • 22. Javascript Libraries Iterating objects in undescore var numObj = { a: 1, b: 2, c: 3, d: 4 }; _.each(numObj, function(value, key) { $(“#myUL”).append( $(“<li>”).text(key+” : “+value)); // adds lis to the ul “#myUL” });
  • 23. Javascript Libraries Some more examples using this list of people objects. var peopleList = [ { name: ”Jeremy”, role: “student” }, { name: ”Tanvi”, role: “instructor” }, { name: ”Pavan”, role: “student” }, { name: “Pramod”, role: “student” } ];
  • 24. Javascript Libraries _.pluck(numObj, “name”); // [“Jeremy”, ”Tanvi”, “Pavan”, “Pramod”] _.where(numObj, { role: “instructor”}); // [{ name: “Tanvi”, role: “instructor” }] _.groupBy(peopleList, {role: "student"}) // { false: [ { name: "Tanvi", role: "instructor" } ], true: [ { name: "Jeremy", role: "student" }, { name: "Pavan", role: "student" }, { name: "Pramod", role: "student" } ]}
  • 25. Javascript Libraries Underscore also provides helper functions. _.isNaN() _.isNull() _.isEmpty() _.isFunction() _.isUndefined() And more
  • 27. Javascript Frameworks Javascript is a great language, but it’s very open, often misunderstood, and ease to write poorly. Using a framework can help you conform to some common programming practices, like MVC.
  • 28. Javascript Frameworks Backbone.js is a great framework that provides Models, Views, and Collections that abstract abstracts a lot of the common functionality of a web app. ◦ ajax calls ◦ event bindings ◦ model changes TODO example