Skip to content

Commit

Permalink
add module methods
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchi committed Aug 4, 2013
1 parent 9efd39d commit 99b9636
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 1 deletion.
21 changes: 21 additions & 0 deletions general-patterns/global-import.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: The Problem with Globals
* Description: You can pass some global objects inside a function.
*/

(function ($, YAHOO) {
// now have access to globals jQuery (as $) and YAHOO in this code
}(jQuery, YAHOO));

// References
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html
</script>
</body>
</html>
3 changes: 2 additions & 1 deletion object-creation-patterns/module2.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
}(); // the parens here cause the anonymous function to execute and return


// reference: http://yuiblog.com/blog/2007/06/12/module-pattern/
// Reference
// http://yuiblog.com/blog/2007/06/12/module-pattern/

</script>
</html>
26 changes: 26 additions & 0 deletions object-creation-patterns/module3-augmentation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Module Pattern - Augmentation
Description: This pattern import modules, and add properties, then export it. It has adventage for developing large applications.
*/

var MODULE = (function (my) {
my.anotherMethod = function () {
// added method...
};

return my;
}(MODULE));


// Reference
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

</script>
</html>
26 changes: 26 additions & 0 deletions object-creation-patterns/module4-loose-augmentation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Module Pattern - loose Augmentation
Description: This pattern import modules, and add properties, then export it. It has adventage for developing large applications.
*/

var MODULE = (function (my) {
my.anotherMethod = function () {
// added method...
};

return my;
}(MODULE || {})); // adding a default object


// Reference
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

</script>
</html>
28 changes: 28 additions & 0 deletions object-creation-patterns/module5-tight-augmentation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Module Pattern - Tight Augmentation
Description: This pattern import modules, and add properties, then export it. It has adventage for developing large applications. And also have the authorities to old module method, and override a new one.
*/

var MODULE = (function (my) {
var old_moduleMethod = my.moduleMethod;

my.moduleMethod = function () {
// method override, has access to old through old_moduleMethod...
};

return my;
}(MODULE));


// Reference
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

</script>
</html>
36 changes: 36 additions & 0 deletions object-creation-patterns/module6-clone-inheritance.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Module Pattern - clone and inheritance Augmentation
Description: This pattern import modules, and add properties, then export it. It has adventage for developing large applications. And the new module object will inheritance the old one.
*/
var MODULE_TWO = (function (old) {
var my = {},
key;

// let my object inherience property
for (key in old) {
if (old.hasOwnProperty(key)) {
my[key] = old[key];
}
}

var super_moduleMethod = old.moduleMethod;
my.moduleMethod = function () {
// override method on the clone, access to super through super_moduleMethod
};

return my;
}(MODULE));


// Reference
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

</script>
</html>
35 changes: 35 additions & 0 deletions object-creation-patterns/module7-cross-file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Module Pattern - cross-file private Augmentation
*/

var MODULE = (function (my) {
var _private = my._private = my._private || {},
_seal = my._seal = my._seal || function () {
delete my._private;
delete my._seal;
delete my._unseal;
},
_unseal = my._unseal = my._unseal || function () {
my._private = _private;
my._seal = _seal;
my._unseal = _unseal;
};

// permanent access to _private, _seal, and _unseal

return my;
}(MODULE || {}));


// Reference
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

</script>
</html>
25 changes: 25 additions & 0 deletions object-creation-patterns/sub-module.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Sub Module Pattern
Description: create a sub module.
*/

MODULE.sub = (function () {
var my = {};
// ...

return my;
}());


// Reference
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

</script>
</html>

0 comments on commit 99b9636

Please sign in to comment.