Modern JavaScript Develop And Design

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 7: User-Defined Functions II
The Web Warrior Guide to Web Design Technologies
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ for Engineers and Scientists Third Edition
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
Julian on JavaScript: Functions Julian M Bucknall, CTO.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
The Object-Oriented Thought Process Chapter 03
Programming Logic and Design Seventh Edition
CHAPTER 10 JAVA SCRIPT.
Classes (Part 1) Lecture 3
2 Chapter Classes & Objects.
Chapter 7: User-Defined Functions II
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Chapter 5 Conclusion CIS 61.
CNIT 133 Interactive Web Pags – JavaScript and AJAX
JavaScript: Functions
Scope, Objects, Strings, Numbers
JavaScript Syntax and Semantics
Function There are two types of Function User Defined Function
JavaScript: Functions.
Functions Declarations, Function Expressions and IIFEs
Chapter 5 - Functions Outline 5.1 Introduction
Object-Oriented Programming: Classes and Objects
Functions.
User-Defined Functions
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 5 - Functions Outline 5.1 Introduction
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Modern JavaScript Develop And Design
Introduction to Classes
Chapter 4 void Functions
Chapter 6 - Functions Outline 5.1 Introduction
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
6 Chapter Functions.
Introduction to Classes and Objects
PHP.
Modern JavaScript Develop And Design
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
CS5220 Advanced Topics in Web Programming JavaScript Basics
Chapter 7: User-Defined Functions II
Topics Introduction to Functions Defining and Calling a Function
Methods.
Method of Classes Chapter 7, page 155 Lecture /4/6.
ITM 352 Functions.
CPS125.
Presentation transcript:

Modern JavaScript Develop And Design Instructor’s Notes Chapter 7- Creating Functions Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman

Objectives Define and invoke simple functions Define and invoke functions that take arguments Validate function parameters Understand how different types of values are passed to functions Return a value from a function

More Objectives Comprehend variable scope Appreciate that functions in JavaScript are another type of value Recognize the meaning of the special this keyword Perform some of the fancier things one can do with functions in JavaScript

Function Theory Functions are used for commonly repeated chunks of code. Can speed development time. Should have a “black box” mentality. Can be used to protect data from being manipulated or accessed inappropriately.

Defining Functions function functionName() { // Function body. } A function definition: keyword function, followed by the function’s name, followed by parentheses, and the function’s body goes between curly braces. The function is called by using the function’s name, followed by the parentheses.

Passing Values function functionName(someVar) { // Function body. } functionName(someValue); function functionName(someVar, someOtherVar) { // Function body. } functionName(someValue, someOtherValue); To have a function take an argument, name one or more variables, separated by commas, between the parentheses of the function definition. Don’t use var for function parameter variables.

Validating Parameters Functions do not check types. Functions do not check the number of parameters. Parameters cannot have default values. JavaScript is weakly typed, so any type of value can be passed to any function parameter. JavaScript will not through an error if a mismatched number of values is passed.

Validating Parameters function add(x, y) { if ( (typeof x == 'number') && (typeof y == 'number') ) { x + y; } Check the parameter’s type to validate it. A parameter will have the value undefined if not passed a value. In more advanced JavaScript programming, you can write a function so that it accepts a variable number of arguments.

How Values are Passed By Value By Reference Numbers Strings Booleans Dates Arrays Objects Simple values are passed to functions by value, which means a copy of the value is passed, not the original variable itself. Thus, changes to the value within the function has no impact on the variable outside of it. Complex values are passed to functions by reference, which means a reference to the original variable is passed to the function, not a copy of the variable’s value. Thus, changes to the value within the function DOES affect the variable outside of it. Complex variables are one way to get data out of a function. Note that this behavior is the same whether or not the arguments and parameters have the same names as each other.

Returning Values function functionName() { // Function body. return something; } var check = functionName(); Use the return statement to return a value. The value can be a literal or a variable. The value can be of any type. You can have multiple return statements but the function terminates as soon as one is executed. If a function does not return any value, it automatically returns undefined.

Variable Scope Realm in which a variable exists. Global scope Local scope Global scope is the largest, default scope. Function definitions create local scope. Each function has its own scope. Where and how a variable is defined determines its scope. Generally speaking, global variables should be avoided.

Global Variables Can create bugs and conflicts. No access control. Can adversely affect performance.

Variable Scope Variables defined outside of any function are global. Variables defined within a function using the var keyword are local. Function parameters are also local. Variables defined within a function WITHOUT using the var keyword are global. If a global and a local variable have the same name, the local variable will overrule the global one. Bugs can arise when variables are accidentally made global.

Functions as Objects Functions are objects of type Function. A function’s name points to its value, which is its definition. Functions are “first-class” citizens. Very important to understanding JavaScript. Functions in JavaScript are just another type of value. Functions can be used like any other type of value: you can assign the value to a variable, use it as a value to be passed to another function, or even return a function from another function.

Functions as Variable Values window.onload = init; window.onload = function() { // Function body. }; This is a common bit of JavaScript code. In it, a function’s definition is assigned to the onload property of the window object. Taking this a step further, you can just assign the function definition to the property. This is called an anonymous function, as it has no name. Note the use of the semicolon at the end, which terminates the assignment of the value statement.

Functions as Argument Values var someFunction = function() { }; someOtherFunction(someFunction); someOtherFunction(function() { }); Some functions actually need to be passed a function. You can do this in two steps or just one. Just be mindful of the syntax when defining the function inline as in the second example.

Functions That Need Functions forEach() every() some() map() filter() reduce() These array methods have been added in ECMAScript 5. Each requires a user-defined function.

Immediately Invoked Functions // Function body goes here. })(); (function() { var someVar; // Function body goes here. })(); An anonymous function that is also immediately called (when this code is encountered). Creates a temporary function. Can be used to hide variables from the global scope.

Nested Functions function functionName() { // Some function body. function anotherFunctionName() { // This function’s body. } You can define one function within another, because functions are objects and objects can have methods. The inner function is just a method of the outer function object. The inner function has its own scope but has access to the variables local to the parent function, including its arguments, as well as any global variable. The inner function is hidden from the global scope.

Recursive Functions function factorial(n) { if (n <= 1) { return 1; } else { return n * factorial(n-1); } A recursive function is one that calls itself. Useful in situations where the same process may need to be executed to an unknown depth.

Context and this Context or execution context is the context in which a line of code is being executed. Different objects and properties exist in different contexts. The special this variable reflects the context. Often, this refers to the object on which a function was invoked. If a function is not associated with an object, this refers to the global object. this allows an object to refer to its own members.