How to use the extendOwn function from underscore

Find comprehensive JavaScript underscore.extendOwn code examples handpicked from public code repositorys.

underscore.extendOwn is a function in the Underscore.js library that extends the properties of an object with the properties of one or more other objects.

5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
// Creates an object that inherits from the given prototype object.
// If additional properties are provided then they will be added to the
// created object.
_.create = function(prototype, props) {
  var result = baseCreate(prototype);
  if (props) _.extendOwn(result, props);
  return result;
};

// Create a (shallow-cloned) duplicate of an object.
fork icon0
star icon2
watch icon0

How does underscore.extendOwn work?

underscore.extendOwn is a function in the Underscore.js library that takes an object as its first argument, and one or more additional objects as subsequent arguments. It extends the properties of the first object with the properties of the other objects, overwriting any existing properties with the same name. Here is the syntax for underscore.extendOwn: javascript Copy code {{{{{{{ _.extendOwn(destination, *sources); The destination argument is the object that will be extended, and the sources arguments are the objects whose properties will be added to the destination object. underscore.extendOwn works by looping through the properties of each source object and adding them to the destination object. If a property with the same name already exists on the destination object, it will be overwritten with the value from the source object. Here is an example of using underscore.extendOwn to extend an object with properties from another object: javascript Copy code {{{{{{{ class="!whitespace-pre hljs language-javascript">const _ = require('underscore'); const destination = { name: 'John', age: 30 }; const source = { age: 35, address: '123 Main St.' }; _.extendOwn(destination, source); console.log(destination); In this example, we create a destination object with a name and age property. We also create a source object with an age and address property. We then call underscore.extendOwn with destination as the first argument and source as the second argument. The resulting object will have the properties name, age, and address. The age property from source overwrites the age property from destination. Overall, underscore.extendOwn is a useful function in the Underscore.js library for extending the properties of an object with the properties of one or more other objects.

Ai Example

1
2
3
4
5
6
const _ = require("underscore");

const destination = { name: "John", age: 30 };
const source = { age: 35, address: "123 Main St." };
_.extendOwn(destination, source);
console.log(destination);

In this example, we create a destination object with a name and age property. We also create a source object with an age and address property. We then call underscore.extendOwn with destination as the first argument and source as the second argument. The resulting object will have the properties name, age, and address. The age property from source overwrites the age property from destination. The output of this code would be: css Copy code