Skip to content

Commit 5879af5

Browse files
committed
Merge pull request #1 from zhanglin800/master
Add extend method
2 parents f88ca4e + e0aaaa3 commit 5879af5

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

extend.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
void function(global) {
2+
if (!global.Box) {
3+
global.Box = {};
4+
}
5+
}(window)
6+
7+
/**
8+
Merge the contents of two or more objects together into the first object.
9+
10+
@methed Box.extend
11+
@param {Object} [, {Object}] [, {Object}] Objects you want to merge.
12+
@return {Object} Retrun merged object.
13+
@example
14+
Box.extend({a: 'word'}, {a: 'string', b: {key: 'value'}})
15+
**/
16+
void function(Box){
17+
var extend,
18+
_extend,
19+
_isObject;
20+
21+
_isObject = function(o){
22+
return Object.prototype.toString.call(o) === '[object Object]';
23+
}
24+
25+
_extend = function self(destination, source) {
26+
var property;
27+
for (var property in destination) {
28+
if (destination.hasOwnProperty(property)) {
29+
30+
// when destination[property] is object and source[property] also is object
31+
if (_isObject(destination[property]) && _isObject(source[property])) {
32+
self(destination[property], source[property]);
33+
};
34+
35+
if (source.hasOwnProperty(property)) {
36+
continue;
37+
} else {
38+
source[property] = destination[property];
39+
}
40+
}
41+
}
42+
}
43+
44+
extend = function(){
45+
var arr = arguments,
46+
result = {},
47+
i;
48+
49+
if (!arr.length) return {};
50+
51+
for (i = arr.length - 1; i >= 0; i--) {
52+
if (_isObject(arr[i])) {
53+
_extend(arr[i], result);
54+
};
55+
}
56+
57+
arr[0] = result;
58+
return result;
59+
}
60+
61+
Box.extend = extend;
62+
}(window.Box)

0 commit comments

Comments
 (0)