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
Object Oriented Programming In JavaScript
PPTX
Lecture 5: Client Side Programming 1
PPT
PPTX
JavaScript and jQuery Basics
KEY
PPT
Java script
PPTX
JavaScript Fundamentals & JQuery
PDF
jQuery -Chapter 2 - Selectors and Events
Object Oriented Programming In JavaScript
Lecture 5: Client Side Programming 1
JavaScript and jQuery Basics
Java script
JavaScript Fundamentals & JQuery
jQuery -Chapter 2 - Selectors and Events

What's hot (20)

PPT
OOP in JavaScript
PDF
Java script tutorial
PPTX
Java script
PPT
JavaScript & Dom Manipulation
PPT
JQuery introduction
PPTX
jQuery PPT
PPTX
PPTX
jQuery
PPTX
jQuery
PDF
jQuery - Chapter 5 - Ajax
PPTX
Object Oriented JavaScript - II
PPT
JavaScript: Ajax & DOM Manipulation
PPTX
jQuery Presentation
PPT
Learn javascript easy steps
PDF
jQuery Introduction
PDF
Introduction to jQuery
PPT
J Query
PDF
Advanced javascript
PPTX
Tips for writing Javascript for Drupal
OOP in JavaScript
Java script tutorial
Java script
JavaScript & Dom Manipulation
JQuery introduction
jQuery PPT
jQuery
jQuery
jQuery - Chapter 5 - Ajax
Object Oriented JavaScript - II
JavaScript: Ajax & DOM Manipulation
jQuery Presentation
Learn javascript easy steps
jQuery Introduction
Introduction to jQuery
J Query
Advanced javascript
Tips for writing Javascript for Drupal
Ad

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

PDF
KEY
JavaScript Neednt Hurt - JavaBin talk
KEY
User Interface Development with jQuery
PDF
AJS UNIT-1 2021-converted.pdf
PPTX
UNIT 1 (7).pptx
PDF
fuser interface-development-using-jquery
PPT
Javascript Experiment
PPTX
All of Javascript
PPTX
Learning About JavaScript (…and its little buddy, JQuery!)
PPT
Introduction to jQuery
PPT
jQuery with javascript training by Technnovation Labs
PDF
Learning jquery-in-30-minutes-1195942580702664-3
PDF
Rediscovering JavaScript: The Language Behind The Libraries
PDF
Building a JavaScript Library
PDF
jQuery in 15 minutes
PPTX
JavaScript!
PPTX
jQuery
PDF
The Dom Scripting Toolkit J Query
PPT
J query intro_29thsep_alok
PDF
Jquery in-15-minutes1421
JavaScript Neednt Hurt - JavaBin talk
User Interface Development with jQuery
AJS UNIT-1 2021-converted.pdf
UNIT 1 (7).pptx
fuser interface-development-using-jquery
Javascript Experiment
All of Javascript
Learning About JavaScript (…and its little buddy, JQuery!)
Introduction to jQuery
jQuery with javascript training by Technnovation Labs
Learning jquery-in-30-minutes-1195942580702664-3
Rediscovering JavaScript: The Language Behind The Libraries
Building a JavaScript Library
jQuery in 15 minutes
JavaScript!
jQuery
The Dom Scripting Toolkit J Query
J query intro_29thsep_alok
Jquery in-15-minutes1421
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Business Ethics Teaching Materials for college
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Pre independence Education in Inndia.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharma ospi slides which help in ospi learning
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial diseases, their pathogenesis and prophylaxis
PPH.pptx obstetrics and gynecology in nursing
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Business Ethics Teaching Materials for college
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Pre independence Education in Inndia.pdf
Basic Mud Logging Guide for educational purpose
Pharma ospi slides which help in ospi learning
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Open Quiz Monsoon Mind Game Final Set.pptx
human mycosis Human fungal infections are called human mycosis..pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Structure & Organelles in detailed.
Open folder Downloads.pdf yes yes ges yes
FourierSeries-QuestionsWithAnswers(Part-A).pdf

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