Giter Site home page Giter Site logo

Comments (10)

jdalton avatar jdalton commented on June 14, 2024

@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.

jdalton avatar jdalton commented on June 14, 2024

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.

Raynos avatar Raynos commented on June 14, 2024

@jdalton https://gist.github.com/3300375

from lodash.

jdalton avatar jdalton commented on June 14, 2024

@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.

Raynos avatar Raynos commented on June 14, 2024

@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.

jdalton avatar jdalton commented on June 14, 2024

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.

jdalton avatar jdalton commented on June 14, 2024

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.

adrai avatar adrai commented on June 14, 2024

@jdalton Your are right, in this case i need _.extend({}, ...).
For me it's ok...
You make a good job! :-)

from lodash.

 avatar commented on June 14, 2024

@jdalton

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.

lock avatar lock commented on June 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.