JavaScript objects, functions, and events

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Wed/Fri Week 2 Functions! What are they? What do they look like in JavaScript? What are they good for? How do I use them? Some examples… Mini-Lab 1!!!
JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
1 JavaScript
Sahar Mosleh California State University San MarcosPage 1 JavaScript Basic.
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Understanding JavaScript and Coding Essentials Lesson 8.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
JavaScript: Conditionals contd.
BIT116: Scripting Lecture 05
Precedence Operators Error Types
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Learning to Program D is for Digital.
Loops BIS1523 – Lecture 10.
JavaScript: Functions
Retrieving information from forms
JavaScript.
Intro to PHP & Variables
Introduction to JavaScript
Lecturer: Mukhtar Mohamed Ali “Hakaale”
More Selections BIS1523 – Lecture 9.
PDO Database Connections: Getting data out of the database
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
A second look at JavaScript
Number and String Operations
A (gentle???) Introduction to JavaScript
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
T. Jumana Abu Shmais – AOU - Riyadh
Programming in JavaScript
Functions, Regular expressions and Events
Sending a text message (and more)
Form Validation (with jQuery, HTML5, and CSS)
Form Validation, Part 2 (with jQuery, HTML5, and CSS)
JavaScript and the DOM MIS 2402 Jeremy Shafer Department of MIS
Numbers, strings and dates in JavaScript
An Introduction to Animation
MIS JavaScript and API Workshop (Part 2)
An Introduction to JavaScript
Programming Control Structures with JavaScript Part 2
Programming in JavaScript
Getting started with jQuery
An introduction to jQuery
Conditional Statements with JavaScript
Loops and Arrays in JavaScript
Introduction to JavaScript
Tutorial 10: Programming with javascript
An introduction to jQuery
Introduction to JavaScript
Introducing JavaScript
Introduction to MIS2402 MIS MIS2402 Jeremy Shafer Department of MIS
Programming Control Structures with JavaScript
An introduction to jQuery
Introduction to AJAX and JSON
Sending a text message (and more)
Strings and Dates in JavaScript
CMPT 120 Lecture 3 - Introduction to Computing Science – Programming language, Variables, Strings, Lists and Modules.
Web Programming and Design
Functions, Procedures, and Abstraction
Retrieving information from forms
JavaScript and the DOM MIS 2402 Maxwell Furman Department of MIS
Presentation transcript:

JavaScript objects, functions, and events MIS 2402 Jeremy Shafer Department of MIS Fox School of Business Temple University

Advisory! This lecture presents some *big* ideas that will be very important in the days ahead. It’ll be extra important to think both in terms of generalities (What is it we are trying to do? And why?) and in specifics (How do we express these ideas in JavaScript? What’s the syntax?) Every lecture is important, but this one in particular will lay the groundwork for the rest of the semester. Functions are even more important than loops!

Let’s talk about… $ Before we begin … What is that crazy $ thing we keep seeing? We saw it in week 4 … $("#quantity_of_gnomes").html(gnomes_needed); We saw it in week 5… $('#answer').append(the_li_tag); What does it all mean?

The $ function, explained OK, it’s time to come clean. The $ function is really at the heart of something called jQuery. Next week we’ll learn more about what jQuery is, and what it can do. But, for now, think of it this way: the “$” means “Select”. So, when we say: $('#mymessage').html('Hello world!’); The browser selects the tag with the id “mymessage” ! Now take action on the tag you found. We haven’t seen the last of the $ function!

Agenda Talk about what functions are Talk about what objects are Talk about the things that make objects special: properties, methods and events Introduce you to the only events you will ever need (in this course, anyway) Equip you with the ability to make your own functions Discuss why you might want to do that

What’s a function? In mathematics, a function is one or more rules that are applied to an input in order to generate an output. The input is the number or value put into the function. The output is the number or value that the function generates.

What is a function? (2) In programming, a function is a collection of statements grouped together in such a way that they can receive zero, one, or more pieces of input. One or more programming statements are then used by the function to generate some output. Here are some examples of functions: var feet_to_inches = function(HowManyFeet){   var x = HowManyFeet * 12;   return x; } var convertToCelcius = function(degrees_fahrenheit){   var the_answer = (degrees_fahrenheit - 32) * ( 5 / 9 );   return the_answer; }

What is a function? (3) The last two examples illustrated functions which returned a value. In JavaScript, sometimes we don’t worry about what value the function returns. Occasionally, we just want to group a bunch of commands together because it is it convenient to do so. In those cases we leave the return value undefined. For example: var bottles_of_beer_song = function(iterations){   for (var i = iterations; i > 0; i--){     console.log(i + ' bottles of beer on the wall, ' );     console.log(i + ' bottles of beer!');     console.log('Take one down, pass it around, ' );     console.log((i - 1) + ' bottles of beer on the wall!');     console.log(' '); //print a blank line   } } See fun01.html and fun02.html in funtimes.zip Don’t forget to set and use breakpoints in the Chrome Developer Tools!

Objects

What’s an object? In JavaScript, an object is known as a complex data type. It’s “complex” because, unlike primitive data types (like number and string) objects can hold more than one value. We call the values stored in objects, properties. What makes objects even more special is that they can have methods and events. Methods enable the object to perform actions. Methods are like verbs. Events describe what can happen to an object. Events are like triggers. Being able to specify properties, methods, and events makes the object data type a very powerful tool for any programming task.

An hypothetical example – a car Properties car.color = “red”; car.fuel_type = “regular”; car.engine_type = “v6”; Methods: car.unlockDoors(key); car.applyBreak(speed); car.turnIgnition(key,position); car.changeGear(gearType); car.isEngineRunning(); car.releaseBrake(speed); Events: car.onObstacle( function (){ takeEvasiveAction() } ); With this convention, anything can be expressed as an object.

Objects are everywhere: Strings Properties and methods of a String object Take note: name.length is a property. It has a single value. Meanwhile, name.toLowerCase() is a method. The method does something. Method names all end with a pair of parenthesis.

Objects are everywhere: Numbers Methods of a Number object Congratulations. You are now part of the very secret society that understand that *everything* in JavaScript is object-oriented.

The only events you need… Recall that events are things that happen. In this course, we will limit ourselves to two events. #1. The “click” event. The “click” event takes place when a tag (usually a button) is clicked. We can use jQuery to detect the click event, like this: $(‘#some_id_here’).click(); Here we are identifying the tag being clicked. When the click event happens, the command inside these parenthesis executes.

But wait, there’s more… When we specify the commands that are to be executed in response to the click event, we *must* bundle them together as a single function. This is true if we have one command to execute or many. $(‘#some_id_here’).click(); Like this. $(‘#some_id_here’).click(function(){ // … all the commands go here });

The “document ready” event. #2. The “document ready” event takes place when an html document is completely loaded into the browser. We can use jQuery to detect the document ready event, like this: $(document).ready(); I am ready! Again the function to execute goes inside these parenthesis. See fun03.html, fun04.html and fun05.html)

Putting it all together In fun06.html let’s write some HTML and JavaScript that waits for a user to click on a button. When the user clicks on the button, prompt the user for degrees Fahrenheit and calculate the degrees Celsius. Define a function named convertFtoC to do the math for you.

Why would you want to do all that? Functions in JavaScript (or any other programming language) allow us to “divide and conquer”. They provide a mechanism to break large, complicated problems down into a collection of smaller, simpler problems. Functions allow use assign meaningful names to blocks of code. For example: the name convertFtoC is more understandable that y = (x - 32) * (5/9); Functions help us avoid writing the same sort of code, over and over. Once I write a convertFtoC function, why would I ever write that formula ever again? I shouldn’t have to.

Next time… Next time we’ll practice writing functions to do some calculations for us. We’ll integrate these simple calculations with buttons on a web page.