JavaScript Closure, Scope

Local Variables

  • Variables declared within a JavaScript function, become LOCAL to the function.
  • Local variables have local scope: They can only be accessed within 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 starts, and deleted when the function is completed.

Global Variables

  • A variable declared outside a function, becomes GLOBAL.
  • A global variable has global scope: All scripts and functions on a web page can access it. 
  • In Browser JS Global scope is window

Automatically Global

  • If you assign a value to a variable that has not been declared (var), it will automatically become a GLOBAL variable.
  • This code example will declare carName as a global variable, even if it is executed inside a function.

Use Strict

  •  "use strict";  Defines that JavaScript code should be executed in "strict mode"
  • The purpose of "use strict" is to indicate that the code should be executed in "strict mode"
  • With strict mode, you can not, for example, use undeclared variables
"use strict";
x = 3.14;       // This will cause an error (x is not defined)

JavaScript Hoisting

  • Hoisting is JavaScript's default behavior of moving declarations to the top
  • In JavaScript, a variable can be declared after it has been used
  • In other words: a variable can be used before it has been declared
  • Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).
x = 6;
x = x * 2 + 1;
var x;
var x;
x = 6;
x = x * 2 + 1;

The same

Lifetime of JavaScript Variables

  • The lifetime of a JavaScript variable starts when it is declared.
  • Local variables are deleted when the function is completed.
  • Global variables are deleted when you close the page.
  • Function arguments (parameters) work as local variables inside functions.

Closure

  • JavaScript variables can belong to the local or global scope
  • Private variables can be made possible with closures
  • A closure is created when an inner function is made accessible from outside of the function that created it

Nested Functions

  • All functions have access to the global scope.  
  • In fact, in JavaScript, all functions have access to the scope "above" them.
  • JavaScript supports nested functions. Nested functions have access to the scope "above" them.
  • In this example, the inner function plus() has access to the counter variable in the parent function:

return function

self-invoking

CLOSURE modules

Validation

Home work

https://learn.javascript.ru/functions-closures Замыкания, область видимости

John Resig - Secrets of the JavaScript Ninja - Книга

http://gabdrahimov.ru/javascript-oglavlenie JS Учебник

http://habrahabr.ru/post/38642/ Замыкания в JavaScript

https://developer.mozilla.org/ru/docs/Web/JavaScript/Closures Замыкания

 

TASK

You can find the task on OneDrive.

THANKS FOR YOUR ATTENTION

JAVASCRIPT CLOSURE, SCOPE

Made with Slides.com