Function Junction Flashcards
Short def. of a closure
Function that returns references to objects from its local scope.
- Typically returns an IIF
T/F: A method’s context (this) is bound to the object that it is a method of
False. A calling function can give ‘function context’ to a method.
Another name for ‘context’ in javascript
“This” = context
How is ‘this’ defined in strict mode?
Strict mode requires an assigned context, or ‘this’ is undefined. It does not default to global object.
Function w/ Call – what’s happening?
myFunction.call(x, 1,2)
myFunction will be executed with the properties of ‘x’ and other given parameters
Function w/ Apply – what’s happening?
myFunction.apply(x, [1,2])
this = ‘x’ and the rest of the parameters are given in an array
* Use if the other parameters you wanted to send were already in an array?
What is the syntax diff between declaring a function and invoking a function?
Declaration: uses function keyword to define a function
Invoking: uses ( ); and returns a value
What is a function expression? Why is this a significant distinction?
A function assigned to a variable, not defined with formal function definition.
+ Allows reassignment of variable
+ Assigned function can be anonymous
+ Function expressions are not hoisted b/c they follow variable scope rules
How do you invoke an anonymous function?
Assign to variable (create function expression) and call with variable name
- End with semi-colon to execute
Syntax for a self-invoking function
Entire function wrapped in ( ),followed by ( );
* Cannot be used on function declaration
(function( ){ …stuff…})( );