Skip to content

Merge two or more JavaScript objects together. It's jQuery.extend() without all the jQuery.

License

Notifications You must be signed in to change notification settings

morgant/extend

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

extend

Merge two or more objects together into the first object. This is a vanilla JavaScript equivalent to jQuery's .extend() method, retaining the same functionality & method signature.

This project was forked from Chris Ferdinandi's original extend and updated to match jQuery.extend()'s method signature. This was done to limit confusion & frustration if switching to & from jQuery & vanilla JS when using the extend() function, but also to gain the benefit of jQuery's excellent documentation on the method. The major difference between this version and Chris's original is that objects will be merged into the first object (which is also returned for easy call chaining), while Chris's doesn't modify any of the original objects and instead returns a new object with the others merged into it.

Download extend


Many thanks to Chris Ferdinandi for the original code. Check out his original blog posts regarding his implementation of extend():

Also check out his "Vanilla JS Guidebook" and level-up as a web developer. 🚀


Getting Started

1. Include extend on your site.

Include the code in your script, or load it as an external file.

var extend = function () {
	...
};

2. Extend your objects

Pass in two or more objects to extend. For a deep or recursive merge, set the first argument to true.

// Example objects
var object1 = {
    apple: 0,
    banana: { weight: 52, price: 100 },
    cherry: 97
};
var object2 = {
    banana: { price: 200 },
    durian: 100
};
var object3 = {
    apple: 'yum',
    pie: 3.214,
    applePie: true
};

// Modify object1 by combining two or more objects into it
var object1Shallow = extend( object1, object2, object3 );
var object1Deep = extend( true, object1, object2, object3 );

// Create a new object combining two or more objects into it
var newObject = extend( {}, object2, object3 );

Browser Compatibility

extend works in all modern browsers, and IE 6 and above.

How to Contribute

Please review the contributing guidelines.

License

The code is available under the MIT License.

About

Merge two or more JavaScript objects together. It's jQuery.extend() without all the jQuery.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%