Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*FUNCTIONS:
- Functions are blocks of code that hold 'instructions' telling the
- computer what to do and how to get it done.
- They go through two phases:
- 1. they are declared with the keyword function followed by a set of parentheses
- and curly braces.
- -The instructions for what to do go inside the curly braces.
- 2. they are then called/invoked/executed by placing
- it outside of the function
- Sometimes functions take parameters/arguments; these are values or
- 'information' that the function needs in order to get the job done.
- Parameters are basically named variables while arguements are the actual
- values used.
- */
- //Example:
- /*This function passes two numbers as arguments, adds them together
- and then returns the sum.
- function add(numOne, numTwo) {
- var sum = numOne + numTwo;
- return sum;
- }
- console.log(add(1, 2)); //prints the number 3
- */
- /*
- There are different types of functions including named functions,
- anonymous functions and function expressions
- NAMED FUNCTIONS:
- Are just functions declared with a name. They have names
- so that they can be called again later on in the program.
- */
- //Example:
- /*This function is called subtractTwo; it takes a number
- subtracts 2 from it and returns the answer.
- */
- 'use strict';
- function subtractTwo(num) {
- var product = num - 2;
- return product;
- }console.log(subtractTwo(7)); //prints 5
- /*ANONYMOUS FUNCTIONS:
- Are declared without a name; they are typically used for
- passing through other functions
- */
- //Example:
- function printNumString(num, action) {
- console.log(action(num));
- }printNumString(2, function (num) {
- return num.toString();
- });
- //prints the number 2 as a string
- /*FUNCTION EXPRESSIONS:
- When an anonymous function is assigned to a variable
- or constant.
- **YOU CAN ONLY CALL A FUNCTION EXPRESSION AFTER IT HAS BEEN
- ASSIGNED NOT BEFORE**
- */
- //Example:
- var add = function add(numOne, numTwo) {
- return numOne + numTwo;
- };
- var sum = add(3, 3);
- console.log(sum); //prints 6
- /*SCOPE:
- When we talk about scope we're talking about whether
- we are inside or outside of a function and what information
- we have access to depending on where we are.
- 1.There is the global scope(outside of a funtion)
- 2.There is the local/block scope (inside of a function)
- children have access to the parent's information but
- the parent does NOT have access to the child's
- */
- //Example:
- var a = 'hi';
- function printString() {
- a = 'hi there';
- }console.log(a);
- /*does NOT throw an error because child has access to
- variable a in parent scope and can change value*/
- function printString() {
- var a = 'hi';
- console.log(a);
- }
- console.log(a);
- /*throws an error because a is declared
- within the function; it is local scoped*/
- /*CLOSURE:
- Functions can carry references to variables in its
- parent scope inside of its definition body.
- The function forms a closure around the environment
- in which it was defined. That means function called
- has access to variables of its parent scope.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement