How does the extend() function work in jQuery?

前端 未结 6 1765
时光取名叫无心
时光取名叫无心 2020-12-12 11:04

I saw this in a plugin:

var options = $.extend(defaults, options); 

How does it work?

What does extend() do?

6条回答
  •  生来不讨喜
    2020-12-12 11:20

    It merges the content of one object to another. If we pass two objects, second object properties are added to the first object / first parameter

    Ex: $.extend(object1, object2);
    

    Now object1 contains properties of object2

    If we want to merge two objects, then we need to pass empty object in the first parameter

    Ex: var newObject = $.extend({}, object1, object2);
    

    Now newObject contains both properties of object1 and object2.

提交回复
热议问题