Comments (10)
@adrai Thanks for reporting. This is one of the reasons I released as RC because I wanted to check the usage of _.clone
with objects created from non-Object
constructors.
So the docs for _.clone
state that
Functions, DOM nodes, arguments objects, and objects created by constructors other than Object are not cloned unless they have a custom clone method.
I don't clone objects created from non-Object
constructors because it wouldn't be cloning the inheritance and instead would flatten it to a vanilla object. The workaround would be to add a Wrappy.prototype.clone
to ensure inheritance but it looks like by your usage that you really needed _.extend({}, ...)
.
I could make shallow clones, not passing deep
, of objects flatten but I wanted to keep it consistent for the initial RC and see what issues it ran into in real code.
from lodash.
I could make shallow clones, not passing deep, of objects flatten but I wanted to keep it consistent for the initial RC and see what issues it ran into in real code.
cc'ing @kitcambridge @mathiasbynens for opinions on shallow clone of objects that don't have Object
as their constructor vs deep
clone.
from lodash.
@jdalton https://gist.github.com/3300375
from lodash.
@Raynos that ES5 approach still misses things that may have been set or processed in the constructor call. The easiest cross-browser consistent way is to punt and allow the dev to implement their own clone
method that then _.clone
will use.
from lodash.
@jdalton of course. You also can't clone closures. I think a sensible default for an object with a prototype is that clone function.
Of course my own opinion is that I never use clone. I use new Constructor(instance.toJSON())
for any of my serialization / deserialization logic
from lodash.
of course. You also can't clone closures. I think a sensible default for an object with a prototype is that clone function.
Nope, I'm going for ES3 support too.
Of course my own opinion is that I never use clone. I use new Constructor(instance.toJSON()) for any of my serialization / deserialization logic
Which is doable with _.clone
if you implement it on:
Constructor.prototype.clone = function() {
return new Constructor(this.toJSON());
};
Underscore's current behavior is to do _.extend({}, source)
internally. I'm trying to gauge if devs actually use _.clone
on object created from non-Objected
constructors.
from lodash.
I'm also considering allowing a callback
argument that _.clone
can use for normally non-cloned values without them having to have a clone
method,
from lodash.
@jdalton Your are right, in this case i need _.extend({}, ...).
For me it's ok...
You make a good job! :-)
from lodash.
cc'ing @kitcambridge @mathiasbynens for opinions on shallow clone of objects that don't have
Object
as their constructor vs deep clone.
Definitely deep clone. I also like the idea of being able to pass a callback
function to clone
.
from lodash.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from lodash.
Related Issues (20)
- curryRight is broken in list.map context HOT 1
- Avoiding Lodash version disclosure HOT 5
- multiple vulnerabilities that exist in lodash.findlast HOT 4
- Attempt structuredClone in cloneDeep before doing manual clone HOT 6
- _.trimStart not work with "/" char
- _.kebabCase and _.camelCase treat some digits as delimiters but not others HOT 1
- async attempt
- lodash-es outdated HOT 2
- feature request: Safe maths functions HOT 6
- Alias `_.tail` to `_.last` HOT 2
- _.deburr() - ț not convert to t HOT 3
- Typescript error HOT 7
- Discussion on Performance Issues of `trimStart` and `trimEnd`(关于 `trimStart` 和 `trimEnd` 性能问题的讨论) HOT 3
- array _.difference bug HOT 1
- CVE-2021-23337 lodash versions prior to 4.17.21 are vulnerable to Command Injection via the template function. HOT 4
- There may be a bug here HOT 2
- When `debounce` or `throttle` options objects have undefined values, those options are unexpectedly set to non-defaults
- Can we include the time and space complexity of each functions in the documentation ?
- Lodash fails to run in edge environments (Vercel edge, CloudFlare workers)
- contribution
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 lodash.