Top Banner
Revealing Module Pattern
33

Revealing Module Pattern

Feb 22, 2016

Download

Documents

onaona

Revealing Module Pattern. But First… the Module Pattern. Established in 2003 by Richard Cornford Made popular by Douglas Crockford Based on IIFEs (Immediate Invoking Function Expression) aka Self Executing Anonymous Function. var someModule = ( function () { … })();. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Revealing Module  Pattern

Revealing Module Pattern

Page 2: Revealing Module  Pattern

But First… the Module Pattern• Established in 2003 by Richard Cornford• Made popular by Douglas Crockford• Based on IIFEs (Immediate Invoking Function Expression)

aka Self Executing Anonymous Function

var someModule = (function() { …})();

Immediate Invoking

Function Expression

Page 3: Revealing Module  Pattern

The Build-Upfunction foo() { // Function Declaration …}

var foo = function() { // Function Expression (classy) …}

var foo = function() { // IIFE …}();

var foo =(function() { // IIFE also …})();

Page 4: Revealing Module  Pattern

IIFEs are not Closuresvar person = (function() {

})(); // Immediately Invoked

var foo = function(x) { var y = 0; var bar = function bar() { return x; } return bar; // Closure};

Page 5: Revealing Module  Pattern

IIFEs are not Closuresvar person = (function() {

})(); // Immediately Invoked

var foo = function(x) { var y = 0; var bar = function bar() { return x; } return bar; // Closure};

var something = foo(5); // something is a reference to the bar() function

Page 6: Revealing Module  Pattern

IIFEs are not Closuresvar person = (function() {

})(); // Immediately Invoked

var foo = function(x) { var y = 0; var bar = function bar() { return x; } return bar; // Closure};

var something = foo(5); // something is a reference to the bar() function

alert(something()); // Alerts 5. This is because bar() is still “alive” within foo and remembers what foo’s X is

Page 7: Revealing Module  Pattern

var someModule = (function() {

})();

So, back to the Module Pattern

Page 8: Revealing Module  Pattern

var someModule = (function() { var x = 5;

})();

Page 9: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; }

})();

Page 10: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return {} // Return Object Literal (JSON)

})();

Page 11: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return {} // Return Object Literal (JSON)

})();

{my_num: 3, a: true, thing: ‘some string’}

key: value

Page 12: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return {} // Return Object Literal (JSON)

})();

{ my_num: 3, a: true, thing: ‘some string’}

Page 13: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return {} // Return Object Literal (JSON)

})();

{ my_num: 3, a: true, thing: ‘some string’, sub_json: {a: 1, b: 23}}

Page 14: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return {} // Return Object Literal (JSON)

})();

{ my_num: 3, a: true, thing: ‘some string’, some_function: function() { … }}

Page 15: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return {

}})();

Page 16: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return { bar: function() { return foo(); } }})();

Page 17: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return { bar: function() { return foo(); } }})();

alert(someModule.x); // Undefined

Private (ish)

Page 18: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return { bar: function() { return foo(); } }})();

alert(someModule.x); // Undefinedalert(someModule.foo()); // someModule.foo not a function

Page 19: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return { bar: function() { return foo(); } }})();

alert(someModule.x); // Undefinedalert(someModule.foo()); // someModule.foo not a function

Public (API)

Page 20: Revealing Module  Pattern

var someModule = (function() { var x = 5; var foo = function() { return x; } return { bar: function() { return foo(); } }})();

alert(someModule.x); // Undefinedalert(someModule.foo()); // someModule.foo not a functionalert(someModule.bar()); // Alerts 5

// When we call someModule, it’s actually the object literal (JSON) that’s being returned. Since that Object literal still has access to the local variables of someModule, it can give us access to the stuff it wants to and essentially deny us from other stuff

Page 21: Revealing Module  Pattern

Why Module Pattern

• Good for namespacing• Pseudo Encapsulation (Private and Public)

The term “Module” in this case just means an isolated container of code.

Page 22: Revealing Module  Pattern

Module Local Scopevar someModule = (function() { var x = 5; // Belongs to the local scope of someModule return { show: function() { alert(x); } }})();

alert(x) // UndefinedsomeModule.show(); // Alerts 5

Page 23: Revealing Module  Pattern

But it has some drawbacksvar someModule = (function() { var x = 0; // Belongs to the local scope of someModule return { init: function(x) { x = x; // this wont work, will it? }, show: function() { alert(x); } }})();

someModule.init(5);someModule.show(); // Alerts 0, what just happened

Page 24: Revealing Module  Pattern

But it has some drawbacksvar someModule = (function() { var x = 0; // Belongs to the local scope of someModule return { init: function(x) { x = x; // this wont work, will it? }, show: function() { alert(x); } }})();

someModule.init(5);someModule.show(); // Alerts 0, what just happened

Page 25: Revealing Module  Pattern

But it has some drawbacksvar someModule = (function() { var x = 0; // Belongs to the local scope of someModule return { init: function(x) { this.x = x; // what if we use “this” }, show: function() { alert(x); } }})();

someModule.init(5);someModule.show(); // Alerts 0, what just happened

Page 26: Revealing Module  Pattern

But it has some drawbacksvar someModule = (function() { var x = 0; // Belongs to the local scope of someModule return { x: 5, init: function(x) { this.x = x; // what if we use “this” }, show: function() { alert(x); } }})();

someModule.init(5);someModule.show(); // Alerts 0, what just happened

Page 27: Revealing Module  Pattern

But it has some drawbacks

So that sucks, but moving on…

Page 28: Revealing Module  Pattern

But it has some drawbacks

• Private and Public methods accessed differently which sucks too

Page 29: Revealing Module  Pattern

But it has some drawbacksvar someModule = (function() { var x = 2; var foo = function() { return x } // Private Method return { init: function(x) { alert(foo() + bar() + x); }, bar: function() { return 3; } }})();

someModule.init(5) // bar() not defined

Page 30: Revealing Module  Pattern

But it has some drawbacksvar someModule = (function() { var x = 2; var foo = function() { return x } // Private Method return { init: function(x) { alert(foo() + bar() + x); }, bar: function() { return 3; } }})();

someModule.init(5) // bar() not defined

Page 31: Revealing Module  Pattern

But it has some drawbacksvar someModule = (function() { var x = 2; var foo = function() { return x } // Private Method return { init: function(x) { alert(foo() + this.bar() + x); }, bar: function() { return 3; } }})();

someModule.init(5) // Alerts 10

Page 32: Revealing Module  Pattern

Revealing Module Patternmakes it all better

var module = (function() { var priv = function() { … }; return { // Return things you want to be public pub: function() { … } }})();

var revealingModule = (function() { var priv = function() { … }; var pub = function() { … }; return { // Reveal things you want to be public pub: pub }})();

Page 33: Revealing Module  Pattern

Revealing Module Patternvar someModule = (function() { var x = 2; var foo = function() { return x } // Private Method var init = function(x) alert(foo() + bar() + x); } var bar = function() { return 3; } return { init: init, bar: bar }})();

someModule.init(5) // Alerts 10