Javascript & JQuery & CSS > extend() - °´Ã¼ º´ÇÕ.
µî·ÏÀÏ : 2017-07-14 16:12
Á¶È¸¼ö : 53,089
extend ¸» ±×·¡µµ È®Àå ÇÑ´Ù´Â ¶æ Àä.
¼·Î ´Ù¸¥ object ȤÀº Ç÷¯±×ÀÎ ÀÛ¼º ½Ã default object¸¦ Á¤ÀÇÇØ µÎ°í »ç¿ëÀÚ°¡ ÀÓÀÇ·Î options °ªÀ» ÁÖ¾úÀ» ¶§
default·Î Á¤ÀÇ µÈ objectÀÇ °ªÀ» optionsÀÇ °ªÀ¸·Î merge Çϴµ¥ ¿ëÀÌÇÏ°Ô »ç¿ëµË´Ï´Ù.
»ùÇà Äڵ带 º¸´Â °ÍÀÌ ÀÌÇØÇϱ⠴õ ½¬¿ï °Í °°½À´Ï´Ù.
var object1 = { apple : 0, banana : {weight : 52, price : 100}, cherry : 97 };
var object2 = { banana : {price : 200}, durian : 100 };
$.extend(object1, object2);
# °á°ú (object2 ÀÇ °ªÀÌ object1ÀÇ °ªÀ» µ¤¾î¾²¸ç º´ÇյǾú½À´Ï´Ù.)
------------------------------------------------------------------------------------------------
object1 = { apple: 0, banana: { price: 200 }, cherry: 97, durian: 100 };
var object1 = { apple: 0, banana: {weight: 52, price: 100}, cherry: 97};
var object2 = { banana: {price: 200}, durian: 100};
$.extend(true, object1, object2);
# °á°ú (object2 °ú object1ÀÌ º´ÇյǾúÀ¸³ª object1ÀÇ ¿øº» °ªÀ» ±×´ë·Î À¯ÁöÇÏ¸ç º´ÇյǾú½À´Ï´Ù.)
------------------------------------------------------------------------------------------------
object1 == { apple: 0, banana: { weight: 52, price: 200 }, cherry: 97, durian: 100 }
var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
var settings = $.extend({}, defaults, options);
# °á°ú
------------------------------------------------------------------------------------------------
settings --> { validate: true, limit: 5, name: bar }
options --> { validate: true, name: bar }
¾Ë°íº¸¸é Âü ½¬¿îµ¥ ¼Ò½º »ó¿¡¼ Àú·± Äڵ带 ¸¸³ª¸é ¹»±î ¸Û¶§¸®°Ô µÈ´Ù´Â;;
¾î°µç api º¸°í È®ÀÎÇÏ´Â°Ô °¡Àå ÁÁÀº ¹æ¹ýÀÎ µíÇÕ´Ï´Ù.