Skip to content

Commit

Permalink
added function patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
chuanxshi committed Dec 29, 2011
1 parent dd62cdd commit 2b6d329
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
43 changes: 43 additions & 0 deletions functions/callback.html
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>
44 changes: 44 additions & 0 deletions functions/callbacks-and-scope.html
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>
19 changes: 19 additions & 0 deletions functions/enforcing-new-in-constructors.html
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>
32 changes: 32 additions & 0 deletions functions/returning-functions.html
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>

0 comments on commit 2b6d329

Please sign in to comment.