forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
howardchi
committed
Aug 4, 2013
1 parent
9efd39d
commit 99b9636
Showing
8 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |