The document provides detailed explanations of JavaScript function invocation patterns, including invoking functions as functions, methods, constructors, and using apply and call methods. It also covers concepts such as function hoisting, self-invoking functions, scopes (local and global), and closures, along with examples for clarity. Additionally, it introduces recursion as a method of function iteration and presents exercises to reinforce the concepts discussed.
Introduction to JavaScript functions and topics covered such as invocation patterns, recursion, and various function types.
Explains different function invocation patterns in JavaScript including function, method, constructor invocation, and usage of call() and apply() methods.
Details on function expressions, their syntax and hoisting behavior in JavaScript regarding callable before they are defined.
Definition and example of self-invoking functions also known as IIFE (Immediately Invoked Function Expression).
Overview of JavaScript scopes, defining local and global scopes, and their importance for data security.
Discussion on local scope, accessibility of variables defined within a function, and scope behavior.
Focuses on global scope, how variables are defined outside functions, and their visibility across the document.
Explanation of function and block scope in JavaScript, variable creation, hoisting, and implications of variables.
Describes function-scoped variables using var keyword and example demonstrating access within the function.
Introduction of block scope using let and const keywords in ES6, along with examples illustrating their usage.
Discusses the creation and deletion of variables and emphasizes the importance of managing global variables.
Defines closures in JavaScript, explaining inner functions' access to outer variables, with an example.
Brief mention of lexical scoping, likely explored through the concept of closures.
Introduction to recursion, defining the process, and providing an example of a factorial function.
Suggests exercises involving recursion for practice, such as summing digits and computing power.
Contact details for Web Stack Academy in Bangalore.
www.webstackacademy.com ‹#›
Function InvocationPatterns
●
The code in a function is not executed when the function is defined
●
Function is executed when it is invoked
●
Functions can be invoked in 4 different ways
– Invoking a function as a “function”
– Invoking a function as a “method”
– Invoking a function with a “Constructor function”
– Invoking a function with a “apply and call”
www.webstackacademy.com ‹#›
Invocation pattern
(invokingas method)
●
When a function is part of an object, it is called a method
●
Method invocation is the pattern of invoking a function
that is part of an object
●
JavaScript will set the this parameter to the object where
the method was invoked on
●
JavaScript binds this at execution (also known as late
binding)
7.
www.webstackacademy.com ‹#›
Invocation pattern
(invokingas method)
<script>
var obj = { firstName: "Smith", lastName: "Doe",
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
// In the example above, this would be set to obj
document.write("Full name is " + obj.fullName());
</script>
8.
www.webstackacademy.com ‹#›
Invocation pattern
(Constructorinvocation)
●
The constructor invocation pattern involves putting the new
operator just before the function is invoked
●
If the function returns a primitive type (number, string, boolean, null
or undefined)
– The return will be ignored and instead this will be returned
(which is set to the new object)
●
If the function returns an instance of Object
– The object will be returned instead of returning this
9.
www.webstackacademy.com ‹#›
Invocation pattern
(Constructorinvocation - 1)
<script>
var Fullname = function(firstname, lastname) {
// create a property fullname
return this.fullname = firstname + ‘ ’ + lastname;
};
var obj = new Fullname(“Tenali”, “Raman”);
document.write("Full name is " + obj.fullname);
</script>
10.
www.webstackacademy.com ‹#›
Invocation pattern
(Constructorinvocation - 2)
<script>
var Fullname = function(firstname, lastname) {
// return object
return { fullname : firstname + ‘ ’ + lastname }
};
var obj = new Fullname(“Tenali”, “Raman”);
document.write("Full name is " + obj.fullname);
</script>
11.
www.webstackacademy.com ‹#›
Invocation pattern
(withapply() and call() methods)
●
JavaScript functions are objects and have properties and
methods
●
The call() and apply() are predefined method, invoke the
function indirectly
●
The call() method uses its own argument list as arguments to
the function
●
The apply() method expects an array of values to be used as
arguments
www.webstackacademy.com ‹#›
Invocation pattern
(byapply() method)
<script>
var obj, arrSum;
function sum(x, y) {
return x + y;
}
arrSum = [5, 4];
document.write("Sum = " + sum.apply(obj, arrSum));
</script>
14.
www.webstackacademy.com ‹#›
Function Expression
Syntax:
varfunc = function (param-1, param-2, . . . , param-n) {
statement(s);
}
●
Variable can be used to invoke the function
●
Above function is an anonymous function (a function
without a name)
15.
www.webstackacademy.com ‹#›
Function Hoisting
●
JavaScriptmoves variable and function declarations to top of
the current scope; this is called hoisting
●
Due to hoisting JavaScript functions can be called before they
are declared
var s = sum (x, y);
function sum (x, y) {
return x + y;
}
function sum (x, y) {
return x + y;
}
var s = sum (x, y);
www.webstackacademy.com ‹#›
Self InvokingFunction
●
Function expressions can be used to self-invoke function
(start automatically without being called)
●
This is done by using parenthesis () -- also known as
function invocation operator
Syntax:
( function_expression ) ( );
18.
www.webstackacademy.com ‹#›
Self InvokingFunction
Example:
( function () {
document.write(“I am self invoking function”);
} ) ( );
●
Such expressions also known as IIFE (Immediately
Invokable Function Expression
19.
www.webstackacademy.com ‹#›
JavaScript Scopes
(what?)
●
Scopedetermines the accessibility (or visibility) of variables,
objects, and functions from different parts of the code at
runtime
●
JavaScript has two types of scope
– Local scope
– Global scope
●
Please note that, in JavaScript, objects and functions are
also variables
20.
www.webstackacademy.com ‹#›
JavaScript Scopes
(why?)
●
Scopeprovides security to data that, in principle, shall be
accessed only by intended part of the code
●
If data is exposed to all parts of program then it can be
modified anytime without your notice which will lead to
unexpected behaviour and results
●
Scope also allow use to use same names in different
functions
21.
www.webstackacademy.com ‹#›
JavaScript Scopes
(LocalScope)
●
Variables defined within a function are local to the function
●
Such variables have local scope and can’t be accessed
outside the function
●
Since local variables are only recognized inside their
functions, variables with the same name can be used in
different functions
●
Local variables are created when a function is invoked
(started), and deleted when the function exits (ended)
22.
www.webstackacademy.com ‹#›
JavaScript Scopes
(LocalScope)
<script>
function square(num) {
var result = num * num; // variable with local scope
}
Square(3); // invoke the function
document.write(“Square of number = ” + result); // Exception
</script>
23.
www.webstackacademy.com ‹#›
JavaScript Scopes
(GlobalScope)
●
Variables defined outside a function have global scope
●
Such variables are visible to all the functions within a
document, hence, can be shared across functions
24.
www.webstackacademy.com ‹#›
JavaScript Scopes
(GlobalScope)
<script>
var result; // variable with global scope
function square(num) {
result = num * num;
}
square(3); // invoke the function
document.write(“Square of number = ” + result);
</script>
25.
www.webstackacademy.com ‹#›
JavaScript Scopes
(Functionand block scopes)
●
Further, in JavaScript, there are two kinds of local scope
– Function scope
– Block scope
●
A block of code is created with curly braces { }
●
Conditional statements (if, switch) and loops (for, while,
do-while) do not create new scope
26.
www.webstackacademy.com ‹#›
JavaScript Scopes
(Howvariables are created?)
●
JavaScript processes all variable declarations before
executing any code, whether the declaration is inside a
conditional block or other construct
●
JavaScript first looks for all variable declarations in given
scope and creates the variables with an initial value of
undefined
●
If a variable is declared with a value, then it still initially has
the value undefined and takes on the declared value only
when the line that contains the declaration is executed
27.
www.webstackacademy.com ‹#›
JavaScript Scopes
(Howvariables are created?)
●
Once JavaScript has found all the variables, it executes
the code
●
If a variable is implicitly declared inside a function -
– Variable has not been declared with keyword “var”
– And, appears on the left side of an assignment
expression
is created as a global variable
www.webstackacademy.com ‹#›
JavaScript Scopes
(GlobalScope)
●
Global variables are not automatically created in "Strict Mode"
<script>
function square(num) {
“use strict”;
result = num * num;
}
document.write(“Square of number = ” + result); // Exception
</script>
30.
www.webstackacademy.com ‹#›
JavaScript Scopes
(GlobalScope)
●
Global variables belong to window object
<script>
function square(num) {
result = num * num;
}
document.write(“Square of number = ” + window.result);
</script>
www.webstackacademy.com ‹#›
JavaScript Scopes
(Functionscope variable)
<script>
function func() {
var x = 10; // function scope variable
{
var x = 20; // function scope variable
document.write("<br>x = " + x); // shall print 20
}
document.write("<br>x = " + x); // shall print 20
}
</script>
33.
www.webstackacademy.com ‹#›
JavaScript Scopes
(Blockscope variable)
●
ECMA script 6 has introduced keywords “let” and “const”
●
Variables declared using these keywords will have block
level scope
●
For these variables, the braces {. . .} define a new scope
34.
www.webstackacademy.com ‹#›
JavaScript Scopes
(Blockscope variable - let)
<script>
function func() {
let x = 10;
{
let x = 20;
document.write("<br>x = " + x); // shall print 20
}
document.write("<br>x = " + x); // shall print 10
}
</script>
www.webstackacademy.com ‹#›
JavaScript Scopes
(Lifetime of variables)
Variable Keyword Scope Life time
Local Var Function ●
Created when function is invoked
●
Deleted when function exits
Let, const Function or
block
●
Created when function is invoked
●
Deleted when function exits
Global var, let,
const
Browser
Window
●
Created when web page is loaded in the
browser window (tab)
●
Deleted when browser window (tab) is closed
var, let,
const
Block ●
Created when block is entered
●
Deleted when block is exited
●
Life time of a variable is the time duration between it’s creation
and deletion
37.
www.webstackacademy.com ‹#›
JavaScript Scopes
(importantnotes)
●
Do not create global variables unless needed
●
Your global variables or functions can overwrite window
variables or functions
●
Opposite is also possible; any function (including window
object) can overwrite your global variables and functions
38.
www.webstackacademy.com ‹#›
Function Closures
●
InJavaScript, an inner (nested) function stores
references to the local variables that are present in the
same scope as the function itself, even after the function
returns
●
The inner function has access to the outer function's
variables; this behavior is called lexical scoping
●
However, the outer function does not have access to the
inner function's variables
39.
www.webstackacademy.com ‹#›
Function Closures
●
Aclosure is an inner function that has access to the outer
function’s variables – scope chain
●
The closure has three scope chains:
– It has access to its own block scope (variables defined
between its curly brackets)
– It has access to the outer function’s variables
– It has access to the global variables
40.
www.webstackacademy.com ‹#›
Function Closures
(Example)
<script>
functiondisp() { // Parent function
var name = "Webstack Academy"; // name is a local variable
displayName() { // displayName() is the inner function, a closure
alert (name); // The inner function uses variable of parent function
}
displayName(); // child function call
}
disp(); // parent function call
</script>
www.webstackacademy.com ‹#›
Recursion
●
Recursion isthe process in which a function is called by
itself
●
Recursion is a technique for iterating over an operation
by having a function call itself repeatedly until it arrives at
a result