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
Showing
4 changed files
with
138 additions
and
0 deletions.
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,43 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>JavaScript Patterns</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<script> | ||
var findNodes = function (callback) { | ||
var i = 100000, | ||
nodes = [], | ||
found; | ||
|
||
// check if callback is callable | ||
if (typeof callback !== "function") { | ||
callback = false; | ||
} | ||
|
||
while (i) { | ||
i -= 1; | ||
|
||
// complex logic here... | ||
|
||
// now callback: | ||
if (callback) { | ||
callback(found); | ||
} | ||
|
||
nodes.push(found); | ||
} | ||
return nodes; | ||
}; | ||
|
||
// a callback function | ||
var hide = function (node) { | ||
node.style.display = "none"; | ||
}; | ||
|
||
// find the nodes and hide them as you go | ||
findNodes(hide); | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>JavaScript Patterns</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<script> | ||
var myapp = {}; | ||
myapp.color = "green"; | ||
myapp.paint = function (node) { | ||
node.style.color = this.color; | ||
}; | ||
|
||
var findNodes = function (callback) { | ||
// ... | ||
if (typeof callback === "function") { | ||
callback(found); | ||
} | ||
// ... | ||
}; | ||
|
||
var findNodes = function (callback, callback_obj) { | ||
// ... | ||
if (typeof callback === "function") { | ||
callback.call(callback_obj, found); | ||
} | ||
// ... | ||
}; | ||
|
||
var findNodes = function (callback, callback_obj) { | ||
if (typeof callback === "string") { | ||
callback = callback_obj[callback]; | ||
} | ||
|
||
// ... | ||
if (typeof callback === "function") { | ||
callback.call(callback_obj, found); | ||
} | ||
// ... | ||
}; | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>JavaScript Patterns</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<script> | ||
function Person() { | ||
var that = (this === window) ? {} : this; | ||
that.name = name; | ||
that.say = function() { | ||
return "I am " + that.name; | ||
}; | ||
return that; | ||
} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>JavaScript Patterns</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<script> | ||
var setup = function () { | ||
console.log(1); | ||
return function () { | ||
console.log(2); | ||
}; | ||
}; | ||
// using the setup function | ||
var my = setup(); // alerts 1 | ||
my(); // alerts 2 | ||
|
||
var setup = function (count) { | ||
var count = 0; | ||
return function () { | ||
return (count += 1); | ||
}; | ||
}; | ||
// usage | ||
var next = setup(); | ||
//next(); // returns 1 | ||
//next(); // returns 2 | ||
//next(); // returns 3 | ||
</script> | ||
</body> | ||
</html> |