0

What are the helper functions available in javascript to copy all properties from one object to another?

I believe we can copy all properties from one object to another by using simply a "for" loop.

But are there any other methods available which we can just chain to an object and achieve the goal?

Subrat Rout
  • 95
  • 2
  • 16
  • What do you mean by a helper function? Are you referring to a prototype function, such as `foo = {a:1}.extend({b:2})`? – Qantas 94 Heavy Oct 19 '14 at 04:34
  • There is no any such functionality available in javascript. Rather you can do it using JQuery or any other javascript library. using JQuery you can make a copy of object like this var copy = $.extend(true, {}, object) – Mohd. Shaukat Ali Oct 19 '14 at 04:36
  • [`Object.create(srcProtoObject)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) will create a *new* object which "chains" the old object. However this is *not* the same as [recursively] copying the properties of an object. – user2864740 Oct 19 '14 at 04:56
  • @Quantas: Yes, I was referring to a prototype function. – Subrat Rout Oct 19 '14 at 05:20
  • @user2864740: I believe Object.create(srcProtoObject) will copy all properties from srcProtoObject recursively.Right? – Subrat Rout Oct 19 '14 at 05:24
  • @SubratRout No, `srcProtoObject` simply becomes the `__proto__` of the new object. The second argument to `create` takes in an object representing property descriptors; not properties/values. – user2864740 Oct 19 '14 at 05:28

0 Answers0