JavaScript Introductin to Functions

11
JavaScript Introduction to Functions Charles Russell Bennu Bird Media

description

This is the slide show to accompany my YouTube video by the same same

Transcript of JavaScript Introductin to Functions

Page 1: JavaScript Introductin to Functions

JavaScript Introduction to Functions

Charles Russell

Bennu Bird Media

Page 2: JavaScript Introductin to Functions

What is a function.

● As Defined by W3Schools A function is a block of code that will be executed when "someone" calls it.● Saved for later and use on demand

Page 3: JavaScript Introductin to Functions

Function invocations

● functionName( parameterList);● Parameters, or arguments, are the values to be

used by the function– Parameters may be expressions, variables,

literals, or objects● Ex: alert('Hello World');● Functions may return values

– name = fullName('Charles', 'Russell');

Page 4: JavaScript Introductin to Functions

Making Your Own Functions

● Functions can be constructed two ways● Statement form● Expression form

Page 5: JavaScript Introductin to Functions

Statement form

function nameOfFunction(parameter1, parameter2, ...) { //statments and expressions

return result; //Optional if no value to be returned

}

● The return statement is optional and may or may not return a value

● Immediate invocation optional with () following end of code block

● If a parameter is declared and not passed no error is generated until an attempt is made to use the missing parameter.

● Some functions are selfcontained and need no arguments

Page 6: JavaScript Introductin to Functions

Functions as Values

● Remember in the discussion on data types● Anything not a primitive is an object

● In JavaScript functions can be used as values, be passed as parameters, and returned as values from functions

Page 7: JavaScript Introductin to Functions

Expression form

var nameOfFunction = function (parameter1, parameter2, ...) {

//statments and expressions

return result; //optional if no value to be returned

}

● Later we will see that in general this is more useful than the statement form.

Page 8: JavaScript Introductin to Functions

Scope

● In this Language variables default to global● This means that any function anywhere can use and

overwrite your values at any time● The var keyword is what is used to limit scope to

the function in which the variable was delcare● There is no block scope only function scope

● This means that any local variable declared in a function can be accessed from anywhere in the function even sub functions

● Variables in parameters have local scope

Page 9: JavaScript Introductin to Functions

this Keyword

● The keyword this is a special object that refers to different important objects based upon context.● When a function is created with the statement form

and is not part of some other Object this refers to the global Object if ES3 this is undefined if ES5 – For browsers this is the Window Object

● When function used as a constructor this refers to current instance of current function

● When as a method this refers to calling object (normally the parent)

Page 10: JavaScript Introductin to Functions

Summary

● Function are code ment to be used later● A function is called by using its name and opening

and closing parents surround 0 or more parameters

● Functions may or may not return a value● Functions are objects and can be used as values

(not just the result)● If a declared parameter is missing or out of order

no error is generated● Functions may be declared in statement or

expression form● Variables default to global scope● var keyword used to limit variable to function

scope● This keyword may refer to the global object, the

calling object, the current instance of the running function or be undefined based upon context

Page 11: JavaScript Introductin to Functions

Introduction to Objects