I would also like to see angular.copy() have an option to preserve not just 
the prototype chain but object type as well. With a default constructor() 
it could call the source's ctor function directly.

Is there a reason for this?

On Friday, November 22, 2013 5:02:21 AM UTC, Nicholas Rawlings wrote:
>
> I'm trying to use Moment.js objects in my AngularJS models, but I’m 
> finding that the Moment.js objects break when they get sent through 
> angular.copy()… some functions (like moment.format) work find, while others 
> (such as moment.toISOString) do not.  The problem seems to lay in the way 
> that angular.copy() effectively destroys the prototype chain of objects 
> passed to it by indiscriminately copying both an object's own properties as 
> well as its inherited ones.
>
> I found that I can get the behavior I expected by modifying angular.copy() 
> as follows:
>
> -      for ( var key in source) {
>
> +      Object.keys(source).forEach(function(key) {
>          destination[key] = copy(source[key]);
> -      }
>
> +      });
>
> +      destination.__proto__ = source.__proto__;
>
>
> By iterating over Object.keys(source), only the source object's own 
> properties are copied to the destination, which is assigned the same 
> prototype as the source.  I'm curious to know what the broader 
> ramifications of such a change would be to AngularJS in general, and why it 
> may or may not be a good idea.
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to