BambooleanLogic
BambooleanLogic

Reputation: 8171

Replace object's properties with that of another

Okay, I'm rewriting this because people kept misinterpreting it.

Given two objects x and y, I wish to replace all properties of x with that of y.

(Note that this is not regular cloning as that implies creating a new object (z). Object x is to be updated, not replaced.)

My idea is that one could do this by using two for-in loops (with hasOwnProperty checks), one for removing everything from x and one for copying everything from y to x, but that feels like an ugly solution somehow.

Is there some more elegant way to do this? Does JavaScript (or possibly jQuery) have some built-in function for copying the state of one object into another object that already exists?

Upvotes: 1

Views: 266

Answers (1)

lex82
lex82

Reputation: 11317

I think jQuery.extend() is what you are looking for.

Upvotes: 1

Related Questions