JS closure and scope

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

what is local scope

A local scope refers to any scope defined past the global scope.

what is lexical scope

Whenever you see a function within another function, the inner function has access to the scope in the outer function, this is called Lexical Scope or Closure - also referred to as Static Scope

what is closure

closure is an inner function that has access to the outer (enclosing) function's variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function's variables, and it has access to the global variables.

what do you have to be careful about when using a closure?

closures have access to the updated values of the outer function's variables. when the inner function is inside a for loop, they can also lead to bugs when the outer function's variable changes with a for loop. if the function is assigned before called, it passes the outer functions vars by ref not value, so the inner function will get a more up to date reference value when it is called. can be fixed with an iife.

what is this in arrow vs regular function?

in pre-es6 this is not assigned a value until an object invokes the function where this is defined. unless it is an arrow function, this will use lexical scope and refers to the originating context of which this is defined. http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/# var firstName = "Peter", lastName = "Ally";​ function showFullName () { // "this" inside this function will have the value of the window object​ // because the showFullName () function is defined in the global scope, // just like the firstName and lastName​ console.log (this.firstName + " " + this.lastName); }​ var person = { firstName :"Penelope", lastName :"Barrymore", showFullName:function () { // "this" on the line below refers to the person object, because the showFullName function will be invoked by person object.​ console.log (this.firstName + " " + this.lastName); } }​ showFullName (); // Peter Ally​​ // window is the object that all global variables and functions are defined on, hence:​ window.showFullName (); // Peter Ally​​ // "this" inside the showFullName () method that is defined inside the person object still refers to the person object, hence:​ person.showFullName (); // Penelope Barrymore

what does "this" refer to?

refers to current scope object and gives access to properties/methods on an object/function.

what is scope

refers to the current context of your code and what references you have access to.

how to make a closure?

use an inner function inside an outer function

how do we make private methods

we can create private scope, by not returning functions. public functions have access to public ones even after returning due to parent scope: var Module = (function () { var privateMethod = function () { }; return { publicMethod: function () { } }; })();

what is global scope

window in the Dom and global object in node. it is the outermost scope that all functions have access to.

how does one create scope?

with an object or function


Ensembles d'études connexes

NUR334 PrepU: Chapter 32: Disorders of Endocrine Control of Growth and Metabolism

View Set

Accounting Exam 3 SmartBooks + Lecture Guides

View Set

Chapter 64: Management of Patients with Neurologic Infections, Autoimmune Disorders, and Neuropathies

View Set

LEGL4900 - Roessing - Exam 2 (Terminology)

View Set