Functions Flashcards
Review of javascript functions
What is the syntax for a function statement/declaration?
What is the name for a function written with this syntax? function calcRectArea(width, height) { return width * height; }
What is the syntax for a function expression?
What is the name for a function written with this syntax? var getRectArea = function(width, height) { return width * height; }
What is a variable’s scope, how does it apply to var, let and const? and what is it?
variables declared with var are function scoped and ones defined with let and const are block scoped. a Block is defined by curly braces.
function () { let example=0; *scope begins*
scope ends}
How do we implement default parameters on a function?
What do they do? Why use it?
example: an option during the function definition to set default values to parameters if none for given for them. function red(scale1='grey',scale2='white',...rest){}
What are rest parameters and how do we implement the use of them on a function?
Why would you use it? and how is it related to the arguments object, also what is that?!
What are we implementing below?
function(a,b,…rest){ all will return their respective values with rest being an array of the ‘rest’ of variables.}
What is closure and give an example.
Explain closure in a loop with var & let. Why use closure?
Closure
Any function that uses a variable from outside the scope involves a closure. Essentially a function/object that is able to preserve private variables even after it’s outer function’s execution context has returned.We use closure to help prevent pollution of the global namespace and it allows us to create private variables.
What is recursion and what does it have to do with a function?
Give an example! Why would we use them?
Recursion is a technique for iterating over an operation by having a function call itself repeatedly until it arrives at a result. Most loops can be rewritten in a recursive style
var counter = 10; while(counter > 0) { console.log(counter--); }
What is an execution context, how does that relate to functions and the global context?
Global execution context cannot be more than one because only one global environment is possible for JS code execution. Functional execution context (FEC): Functional execution context is defined as the context created by the execution of code inside a function. Each function has it’s own execution context.
So nested functions could be refered to as a stack. Also execution context can be looked at as a map of where we are in the execution of code.
Is javascript single threaded?
Well, arguably its not true that Javascript is single threaded if you see from the under hood working of browser JS to your JS code, there are thread pools. By single threaded what they mean(browser end) is your JS runs into a single threaded event loop. There is one single thread that handles your event loop.
What is an IIFE, and give an example
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. (function () { statements })();
It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts.
The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.
What is this and what kind of interactions happen with functions and this?
Why would we use this?
this will make reference to the object it is called in, unless explicitly to do otherwise using call, apply, or bind. Functions do not give a scope reference for this, it will just point to the window object as “this”. in an object named movies, this will refer to the object movies. So by default it points to the window object, implicitly it will point to an object that contains that reference to this, and explicitly it will use the direct reference passed by apply,call or bind! A trick that I like to use is to look at the left side of the function call. The object that is standing before the dot is what the this keyword will be bound to.
What are callbacks, what are they used for and how are they implemented?
Why would we use one?
Callbacks
Callbacks are a way to make sure certain code doesn’t execute until other code has already finished execution.
function greeting(name) { alert('Hello ' + name); }
function processUserInput(callback) { var name = prompt('Please enter your name.'); callback(name); }
processUserInput(greeting);
What is meant by functions in javascript being first class functions?
first class functions functions can be assigned as constants, variables, placed as array elements and even set as values of keys on an object. Additionally, (and most importantly ?!) functions can be returned to and from functions — just like any other data type! (Also refered to as high order functions the returning and taking functioions as arguments)
Give an example of a function being returned to a value of a variable, how do we now execute the new code set to that variable?
Give an example, and explain why we would want to do this.
const addWrapper = () => (x,y) => x + y;
const add = addWrapper();
const sum1 = add (1,2); // 3
// Or we could do it like this
const sum2 = addWrapper()(4,4); // 8
What is a method, how does that apply to functions?
What is the difference between a function and method?
JavaScript methods are actions that can be performed on objects. A JavaScript method is a property containing a function definition. Property.
Difference is that a method is the first execution context attached to an object as a property.. all other nested functions are treated as functions and don’t retain the this keyword unless it is a arrow function.