Comments (5)
deep-assign
suffers from this behavior for some time – unfortunately, the problem is that, simply recursive Object.assign() does not work as hoped.
The problem is that functions etc. are objects too, and we cannot clone them. Using your example:
const a = {x: () => 42}
const b = {x: {y: 1}}
const result = deepAssign({}, a, b);
console.log(result.x()) // should print 42
console.log(result.x.y) // should print 1
Sorry, I'm not aware of any good solution to this, at the moment a.x
is mutated by deep-assign
.
If we take a look at the Object.assign Spec, then we see that Object.assign
does not distinguish functions etc. from plain objects; therefore there is no (sufficient) reason for deep-assign
to do so.
There are many merge tools doing this in a different way, for example:
_.merge
merge-options
– does not mutate anything (disclaimer: I'm the author of it)
from deep-assign.
shouldn't it mutate
b
too?
Well, I do not know. Did you expect that?
from deep-assign.
Well no, I wasn't expecting that
Thanks for the answer @schnittstabil ! :)
I found assign-deep which seems to work well
from deep-assign.
@educastellano Thanks for the link, I was not aware of that project. The crucial difference of deep-assign
and assign-deep
can be seen at assign-deep/index.js line 60 using kind-of
:
var typeOf = require('kind-of');
// …
function isObject(obj) {
return typeOf(obj) === 'object' || typeOf(obj) === 'function';
}
The problem I see here is:
The distinction between objects and non-objects is neither based on the behavior of Object.assign nor on what an object in JavaScript is. Therefore it should be is easy to find some unexpected behavior of assign-deep
too.
I do not want to go into details of assign-deep
, but as far as I can see, it probably would not meet your requirements – assign-deep
also mutates sources:
const src1 = {
b: new Buffer (1),
p: Promise.resolve(42)
};
const src2 = {
b: {c: 666},
p: {q: 666},
};
assignDeep({}, src1, src2);
console.log(src1.b.c) // => 666
// but:
console.log(src1.p.q) // => undefined
from deep-assign.
Good to know... It doesn't seem very appropiate to use kind-of for that library. isObject()
should say if the value is really an object
(not buffer
or boolean
in case of new Boolean()
,...) . /cc @jonschlinkert
from deep-assign.
Related Issues (20)
- Check "support symbols as targets" fails on ava 0.5.0 HOT 1
- Option to not throw TypeError for null or undefined targets HOT 11
- Overwriting array properties. HOT 4
- Simplify HOT 1
- Deep assigning objects with different property types HOT 1
- Does not work with sealed objects HOT 2
- What is the difference deep assigning with JSON.parse? HOT 1
- Assigning an array shorter than previous one results in merged array HOT 2
- deep assign and functions HOT 2
- property with null value in source object not overwriting to same property in target object HOT 6
- Source object get manipulated HOT 3
- Why should this module not be used? HOT 3
- regular expressions are being ignored HOT 2
- Can you add a param(value) which is equal undefined during assigning HOT 1
- Set not being overwritten HOT 3
- Deep-copying arrays HOT 17
- Incorrectly assign object with function as value HOT 2
- Regression in 2.0.0 HOT 11
- test fails: support symbols as targets HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deep-assign.