-
Notifications
You must be signed in to change notification settings - Fork 27.5k
angular.copy should respect hasOwnProperty and not copy entire prototype chain #1427
Comments
Or even better, it would be nice to provide custom copy behavior, like how objects with a forEach property use that instead of the default forEach. Since we're using Angular with Closure, we might have private fields (ending with underscore) and we could skip those for copy(). |
Thinking about this more, maybe it's fine as it is. We can still enrich the model objects, we could just make sure we only add functions and not objects to it. That way JSON.stringify skips the methods and everything just works. If we need objects (not data objects, but objects that the model methods need), we could just capture them in closure and expose getter methods. |
@jpsimons Closing for now as changing |
@pkozlowski-opensource, sorry for necroposting, but that's definitely a bug. Why would anybody want to follow prototype chain while copying? As for the use cases that are affected by this bug I suppose that @jpsimons provided a couple of reasonable ones. Actually I also have hit the same issue with exactly the same two use cases, so I think that they aren't that rare. Do you need a jsfiddle to reproduce it? Or how else can we help fixing it? |
Agree, I need a deep copy function, this angular copy does not work: The prototype methods are copied on the object. This is not a copy. Maybe change the name of the method and provide a real copy. |
I believe it IS a copy. I believe Now, a new |
It's just a change on https://github.com/angular/angular.js/blob/master/src/Angular.js#L556 like:
Can't think of any reason people would want the prototype chain treated as data...
We hit this issue in a couple of places, one we have "extended" ngResource objects with model-methods on them, and those get copied on $save. Not a problem until something references the DOM and it tries to copy the entire document.
Also when we open an edit dialog, we snapshot the current state of the data so that cancel would revert it. We do that using angular.copy and likewise, all the prototype methods get copied which breaks if the DOM is referenced somehow, with a stack overflow.
The text was updated successfully, but these errors were encountered: