JavaScript Introductin to Functions
-
Upload
charles-russell -
Category
Internet
-
view
140 -
download
2
description
Transcript of JavaScript Introductin to Functions
JavaScript Introduction to Functions
Charles Russell
Bennu Bird Media
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
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');
Making Your Own Functions
● Functions can be constructed two ways● Statement form● Expression form
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
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
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.
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
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)
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
Introduction to Objects