Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

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.

varextend=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 objectsvarobject1={apple: 0,banana: {weight: 52,price: 100},cherry: 97};varobject2={banana: {price: 200},durian: 100};varobject3={apple: 'yum',pie: 3.214,applePie: true};// Modify object1 by combining two or more objects into itvarobject1Shallow=extend(object1,object2,object3);varobject1Deep=extend(true,object1,object2,object3);// Create a new object combining two or more objects into itvarnewObject=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 objects together. A vanilla JavaScript equivalent of jQuery's .extend() function.

Resources

Contributing

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages