klevu.extend()

Description: Merge the contents of two or more objects together into the first object.

klevu.extend( target [, object1 ] [, objectN ] )
Returns: Object

  • target – An object that will receive the new properties if additional objects are passed in or that will extend the Klevu namespace if it is the sole argument.
  • object1 – An object containing additional properties to merge in.
  • objectN – Additional objects containing properties to merge in.

klevu.extend( [deep],target [, object1 ] [, objectN ] )
Returns: Object

  • deep – If true, the merge becomes recursive (aka. deep copy). Passing false for this argument is not supported.
  • target – An object that will receive the new properties if additional objects are passed in or that will extend the Klevu namespace if it is the sole argument.
  • object1 – An object containing additional properties to merge in.
  • objectN – Additional objects containing properties to merge in.

When two or more object arguments are supplied to klevu.extend(), properties from all of the objects are added to the target object. Arguments that are null or undefined are ignored.

If only one argument is supplied to klevu.extend(), this means the target argument was omitted. In this case, the Klevu object itself is assumed to be the target. By doing this, you can add new functions to the Klevu namespace. This can be useful for plugin authors wishing to add new methods to Klevu .

Keep in mind that the target object (first argument) will be modified, and will also be returned from klevu.extend(). If, however, you want to preserve both of the original objects, you can do so by passing an empty object as the target:

var object = klevu.extend({}, object1, object2);

The merge performed by klevu.extend() is not recursive by default; if a property of the first object is itself an object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged. This can be seen in the example below by examining the value of banana. However, by passing true for the first function argument, objects will be recursively merged.

Warning: Passing false for the first argument is not supported.

Undefined properties are not copied. However, properties inherited from the object’s prototype will be copied over. Properties that are an object constructed via new MyCustomObject(args), or built-in JavaScript types such as Date or RegExp, are not re-constructed and will appear as plain Objects in the resulting object or array.

On a deep extend, Object and Array are extended, but object wrappers on primitive types such as String, Boolean, and Number are not. Deep-extending a cyclical data structure will result in an error.

For needs that fall outside of this behavior, write a custom extend method instead, or use a library like lodash.

klevu chain types are copied correctly with no reference to source object.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>