-
Notifications
You must be signed in to change notification settings - Fork 634
/
Copy pathiife-pattern.html
49 lines (42 loc) · 1.27 KB
/
iife-pattern.html
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html>
<head>
<title>IIFE Design Patterns in JS</title>
</head>
<script>
/***
* An immediately-invoked function expression (or IIFE, pronounced "iffy") is a JavaScript programming
* language idiom which produces a lexical scope using JavaScript's function scoping.
* Immediately-invoked function expressions can be used to avoid variable hoisting from within blocks,
* protect against polluting the global environment and simultaneously allow public access to methods
* while retaining privacy for variables defined within the function.
*
* ref: https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
* ***/
//IIFE - syntax
(function() {
// statements
})();
// Example
(function(window) {
'use strict';
function Person(name, address) {
this.name = name;
this.address = address;
}
Person.prototype.sayHello = function() {
console.log(this.name + ' says hello');
};
window.Person = Person;
})(window);
function init() {
var PersonObj = new Person('Pradeep', 'Bangalore');
PersonObj.sayHello();
console.log(PersonObj);
}
init();
</script>
<body>
IIFE Design Patterns
</body>
</html>