14

While going through underscorejs's list of methods, I couldn't help but notice a method which I don't remember being there before: extendOwn

The documentation for this method says the following:

extendOwn _.extendOwn(destination, *sources) Alias: assign

Like extend, but only copies own properties over to the destination object.

I understand how .extend() is used and what it does... but for the life of me I cannot understand how it differs from .extendOwn().

I tried using .extend() and then .extendOwn() to extend a few objects just to see if maybe there was something obvious that would happen - but they seem to both produce the same result.

var a = {
    foo: false
};

var b = {
    bar: true
};

// This will produce { foo: false, bar: true }; ..just like _.extend() would =\
_.extendOwn( a, b );

Any insight into this mystery would be greatly appreciated!

2
  • I have not - that's a good idea though thanks Mar 12, 2015 at 23:30
  • heh, so i just read the source. The funny thing is that it boils down to using .keys vs _.allKeys - which in turn is different one uses nativeKeys and the following : for (var key in obj) if (.has(obj, key)) keys.push(key); where the other does not. dunno - seems still a bit of a mystery to me why extendOwn is even necessary. Mar 12, 2015 at 23:33

2 Answers 2

12

"Own properties" is a technical term in JS. An object's own properties are ones that it didn't inherit.

Here's a short snippet that exposes the different behavior of extend and extendOwn:

// lines have length
line = { length: 4 }

// planes have width and inherit length
plane = Object.create(line)
plane.width = 5
plane.length  // 4

// making a cube object, using extend
cube = _.extend({ height: 6 }, plane)
cube.length  // 4

// making a cube object, using extendOwn
notACube = _.extendOwn({ height: 6 }, plane)
notACube.length  // undefined

As you can see, extendOwn only copied properties that were defined directly on the source, whereas extend also copied the ones defined along its prototype chain. Also note the symmetry with _.has:

_.has(plane, 'width')   // true
_.has(plane, 'length')  // false
1
  • 1
    This answer is helpful. The Underscore documentation is really shitty at explaining this.
    – luxon
    Dec 13, 2016 at 22:26
9

So for anyone wondering, a good place to find the answer is here: https://github.com/jashkenas/underscore/search?q=extendOwn&type=Issues&utf8=%E2%9C%93

Update

For anyone interested, the answer is that extendOwn is synonymous with Object.assign with the implementation being a tiny bit different. Underscorejs simply is adding an alternative to it. Rather than overriding assign with a new implementation into Underscorejs and calling it _.assign, they are calling it _.extendOwn (with _.assign being an alias to _.extendOwn).

The reason for this naming convention is understandable, but imho a bit confusing. You see, Object.assign is ES6's official name for the method/logic we know as "extend" (as called by tools such as jQuery and Underscore).

The decision by the Underscore team was that they decided on calling the primary/parent method extendOwn to adhere to their own internal standards. Naming the primary method _.assign would be (to the Underscore's team) counter intuitive as to them, it confuses what "extend" does. By calling it extendOwn, they are saying that this method does the same thing as "extend" but is based on the ES6's implementation of this functionality known as "assign".

Essentially - what they had here was a paradox, and they needed to make a decision. Either they stick with the convention we know as "extend" or they allow "assign" - which would just conflict with that original name (which might also begin to cause people to question why they would still call the other method "extend" rather than assignSomethinghere instead).

Long story short - extendOwn is the Underscore version of ES6's Object.assign. They just named it extendOwn to keep it in alignment with and the same naming convention, which is named extend.

4
  • 4
    I'd guess that hasOwnProperty on MDN might make good reading right about now. Mar 13, 2015 at 0:17
  • The _.has call inside the in loop that you mentioned in your comment is probably just an alias for obj.hasOwnProperty(key). extendOwn property doesn't walk up the prototype but extend does; there won't be any difference if you're just using simple objects (i.e. o = { ... }). Mar 13, 2015 at 1:12
  • 2
    This doesn't answer the question. You've given an account of the history of why things are named as they are, but haven't explained the difference in functionality between the two methods. Mar 20, 2020 at 12:55
  • 1
    re-read it again. the articles linked in the accepted answer (and in the comment above by @muistooshort ) do the justice of actually explaining the differences of functionality question Mar 23, 2020 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.