Unit 10-JavaScript Functions Instructor: Brent Presley.

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Guide To UNIX Using Linux Third Edition
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Review of C++ Programming Part II Sheng-Fang Huang.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
Introduction to JavaScript 11 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 14.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
JavaScript, Fourth Edition
Using Client-Side Scripts to Enhance Web Applications 1.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
Learners Support Publications Classes and Objects.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
JavaScript Tutorial 1 - Introduction to JavaScript WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Introduction to JavaScript – A First.
Java Script User Defined Functions. Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to.
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
CS346 Javascript -3 Module 3 JavaScript Variables.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
JavaScript, Fourth Edition
Unit 12 JavaScript Arrays Instructor: Brent Presley.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Object Oriented Programming Criteria: P2 Date: 07/10/15 Name: Thomas Jazwinski.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
MTA EXAM HTML5 Application Development Fundamentals.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Expressions and Data Types Professor Robin Burke.
JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.
Introduction to JavaScript academy.zariba.com 1. Lecture Content 1.What is JavaScript? 2.JavaScript Pros and Cons 3.The weird JavaScript stuff 4.Including.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Creating Your Own Classes
4. Java language basics: Function
User-Written Functions
Functions Comp 205 Fall ‘17.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Functions Declarations, Function Expressions and IIFEs
Using local variable without initialization is an error.
METHODS AND BEHAVIORS AKEEL AHMED.
Functions, Regular expressions and Events
Web Programming and Design
Web Programming and Design
Presentation transcript:

Unit 10-JavaScript Functions Instructor: Brent Presley

ANONYMOUS FUNCTIONS Anonymous functions –this is what our $ function is –they are given a name by assigning them to a variable –var calc = function{...}

REGULAR FUNCTIONS Regular functions –place functions in the.js file anywhere –may be declared before or after called –parameters are local variables –very similar to the manner you would use a # method

OPTIONAL PARAMETERS all parameters are really optional –some programmers use no parameters –if you know parameters are required, you should pass them in the parameter list JS stores the arguments sent to a function in the predefined arguments object the parameters are what are included in the function declaration and arguments are what is provided in the function call

OPTIONAL PARAMETERS VS OVERLOAD Most languages will overload a method by giving different parameter lists –sort(int i1, int i2) –sort(int i1, int i2, int i3) –sort(string s1, string s2) however, JavaScript has optional parameters & cannot be overloaded –if not passed in, JavaScript will set them to undefined

THE ARGUMENTS ARRAY arguments is an array of all the objects sent to the function arguments.length returns how many arguments there were arguments[i] provides access to the individual arguments (replace i with a 0-based counter/index)

DETERMINE IF AN OPTIONAL PARAMETER IS MISSING Check if arguments.length is less than the number of arguments you expect (alternative) if(paramName===undef ined)

AVG FUNCTION functions with variable numbers of parameters –write an average function that allows any number of arguments.

FUNCTION RETURN VALUES Functions can optionally return a value return returnValue; If a function call expects a return value and none is provided an error will most likely occur If the function does not return a value, you may still include the return; statement, but it is optional. Structured programming has historically dictated that a function should only have one return statement. However, this is no longer the expectation, but if you are using multiple return statements, do it carefully. function-have-only-one-return-statement function-have-only-one-return-statement

CALLING A FUNCTION calling a function that doesn't return a value –funcName(argumentlist); –the argument list is optional if the function doesn't have parameters or if all parameters are optional calling a function that does return a value myVariable = funcName(argumentlist); –or document.writeln("square rt is " + sqrt(myVariable)); //the function return value is used immediately, but not stored

ADDING A METHOD TO A CLASS Remember, in most languages a method is simply a function embedded in a class The built-in JavaScript classes have many predefined methods However, you may want to define a new method and assign it to a class (a prototype class) defining a method for a built-in class is done using anonymous functions

OBJECT METHODS create an object method: access the object method you will describe fullName() as a method of the person object, and fullName as a property

OBJECT METHOD EXAMPLE changeName is the method here to access: myName.changeName("Alan");

OPEN JSTOOLS look at titlecase.js –view source, note prototype modification Defining a method for a built-in class is done using anonymous functions

Replace ClassName with the name of the class you’re adding to (Date, String, Math, etc.) prototype is a required keyword Replace methodName with the name of your choice As with functions, the parameter list is optional As with functions, the return value is optional, but many methods include one Note the similarities between method declarations and function declarations

CALLING/INVOKING THE METHOD Unlike functions, methods must be invoked by first referencing the object you wish to apply the method to. result = objName.methodName(argList); betterString = myString.toTitleCase(); The result variable assignment is not used when the method doesn’t return a value. The argList is optional based on the requirements of the method

In the example, myString is a String variable (an instance of the String class TitleCase is the name of a method defined using the prototype technique described above (it requires no arguments) TitleCase does return a value which is stored in another String variable named betterString

IMPORTING FUNCTIONS Functions are often stored in JavaScript libraries Files containing nothing but JavaScript function(s), with js extension To import a library, making its functions available: javascript is a folder within your website containing JavaScript libraries –scripts is also used as the folder name Note the end script tag is required even though no JavaScript is included between the tags

GLOBAL VARIABLES Global variables are variables defined outside of a function. These variables can be referenced by any function and any code not in a function Code can read or change the variable Use global variables sparingly

LOCAL VARIABLES Local variables are variables defined inside a function Include parameters Only accessible inside that function Generally considered better programming practice to pass data needed by a function as a parameter instead of declaring globally

ASSIGNMENT UNIT 9 JavaScript Functions –lab the rest of today, as well as Thursday –Due one week from today

DEMO Create a function to display the string variable using a global string variable Discuss why global variable not needed here. Write calcAverage function that calculates the average of a global array. Declare array in html page JavaScript Display contents of array and average in html page