forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmix-ins.html
40 lines (35 loc) · 868 Bytes
/
mix-ins.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Mix-ins
Description: copy from any number of objects and mix them all into a new object
*/
function mix() {
var arg, prop, child = {};
for (arg = 0; arg < arguments.length; arg += 1) {
for (prop in arguments[arg]) {
if (arguments[arg].hasOwnProperty(prop)) {
child[prop] = arguments[arg][prop];
}
}
}
return child;
}
var cake = mix(
{eggs:2, large:true},
{butter:1, salted:true},
{flour:'3 cups'},
{sugar:'sure!'}
);
console.dir(cake);
// reference
// http://addyosmani.com/resources/essentialjsdesignpatterns/book/#mixinpatternjavascript
// http://shop.oreilly.com/product/9780596806767.do
</script>
</body>
</html>