-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosures-private-variable.js
More file actions
33 lines (23 loc) · 1.05 KB
/
Copy pathclosures-private-variable.js
File metadata and controls
33 lines (23 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// # Closure
function greeting(){
// private variable "_message" with underscore by naming convention.
var _message = 'message 1 from the private variable 1';
var greet = function(){
console.log(_message) // displays variable in console
}
return greet // returns a function without invoking it
}
var outerGreet = greeting(); // greeting returns a function greet an saves into the variable outerGreet
outerGreet() // outerGreet invokes greet function wich returns a console.log(variable)
function greeting2(){
// private variable "_message" with underscore by naming convention.
var _message = 'message 2 from the private variable 2';
var greet = function(){
return _message // returns variable
}
return greet // returns a function without invoking it
}
var outerGreet2 = greeting2(); // greeting2 returns a function greet an saves into the variable outerGreet2
console.log(outerGreet2()) // outerGreet invokes greet function wich returns a variable
var x = greeting._message
console.log(x) //undefined