Last active
October 3, 2015 06:18
-
-
Save ethertank/2407688 to your computer and use it in GitHub Desktop.
extend() : オブジェクトに別オブジェクトのプロパティを追加(上書き)して返す関数
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
function extend() { | |
if (arguments.length < 2) throw Error('"extend" : Two or more arguments are required.'); | |
for (var i = 1, l = arguments.length; i < l; ++i) { | |
for (var p in arguments[i]) arguments[0][p] = arguments[i][p]; | |
} | |
return arguments[0]; | |
} |
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
/* SYNTAX *//* | |
var extendedObj = extend( baseObj, overwriteObj [,overwriteObj ...] ); | |
*/ | |
/* EXAMPLE *//* | |
var obj3 = {d:4}; | |
var extendedObj = extend( {a:1,b:2}, {c:3}, obj3 ); | |
*/ | |
/* TEST */ | |
var o1 = { a: 1, b: 2, c: 1000 }, | |
o2 = { c: 3, d: 4, e: 5 }, | |
o = extend(o1, o2, { f: 6, g: 7 }); | |
document.write("どや!(・∀・∀・)<br \/>"); | |
for (p in o) { document.write(p + ": " + o[p] + "<br />"); } | |
document.write("<hr \/>"); | |
document.write("元のやつ(・∀・)破壊!<br \/>"); | |
for (p in o1) { document.write(p + ": " + o1[p] + "<br />"); } | |
document.write("<hr \/>"); | |
document.write("例外チェック(ノ゚д゚)ノ 彡┻━┻<br \/>"); | |
try { extend(); } catch(e) { document.write(e + "<br \/>"); } | |
try { extend(o1); } catch(e) { document.write(e + "<br \/>"); } | |
/* OUTPUT *//***************************************************************** | |
どや!(・∀・∀・) | |
a: 1 | |
b: 2 | |
c: 3 | |
d: 4 | |
e: 5 | |
f: 6 | |
g: 7 | |
-------------------------------------------------- | |
元のやつ(・∀・)破壊! | |
a: 1 | |
b: 2 | |
c: 3 | |
d: 4 | |
e: 5 | |
f: 6 | |
g: 7 | |
-------------------------------------------------- | |
例外チェック(ノ゚д゚)ノ 彡┻━┻ | |
Error: "extend" : Two or more arguments are required. | |
Error: "extend" : Two or more arguments are required. | |
******************************************************************************/ | |
/* MEMO *//* | |
最初の引数のオブジェクトのプロパティを2個目以降の引数に与えられたオブジェクトのプロパティで上書き(無い場合は追加)する関数。 | |
3個以上のオブジェクトを受け付けるのが、他の多くの実装と異なる点ではないかと思う。 | |
破壊的な関数とし、またconstructorをチェックしないように変更。Arrayなどにも使用できるようにし、汎用性を高めた。 | |
これに伴い、型に関する例外スローを省略。引数の数に関する例外のみとした。 | |
「破壊的な動作」、「厳密な型チェック無し」への変更については、深い考えがある訳ではなく、他の方の多くの実装を踏襲しただけ。 | |
*/ |
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
Object.prototype.extend || (Object.prototype.extend = function(e) { | |
if (!e && (this !== null) && e instanceof Object !== "object") throw new Error("引数はオブジェクトで!一個だけな!(・∀・)"); | |
for (var p in e) e.hasOwnProperty(p) && (this[p] = e[p]); | |
return this; | |
}); |
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
var o = { a: 1, b: 2, c: 3 }; // object | |
// 元のオブジェクト | |
for (p in o) if (o.hasOwnProperty(p)) document.write(p + ": " + o[p] + "<br />"); | |
document.write("<hr \/>"); | |
// test | |
var e = o.extend({ c: 5, d: 4, e: 5 }); | |
for (p in e) if (e.hasOwnProperty(p)) document.write(p + ": " + e[p] + "<br />"); | |
document.write("<hr \/>"); | |
// 元のオブジェクト | |
for (p in o) if (o.hasOwnProperty(p)) document.write(p + ": " + o[p] + "<br />"); | |
document.write("破壊的にした(・∀・)<hr \/>"); | |
// error test | |
try { new Date().extend(); } | |
catch (e) { document.write(e); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment