1

Please forgive me in advance as I'm sure this is a noob question (I honestly searched google for an hour before I cam here);

Long Story:

Okay, so I'm creating an application that lists vehicles stored in a database. I have a JavaScript class called Car that is instantiated for each car listing. The class contains default values for everything (make, model, year, color, price, etc) and has a function to use a jQuery ajax request to have my php return the actual values for the car. I pass it with JSON and have a consistent naming convention, so my xhr object in my success function is basically a copy of the car object, only with the correct values. I'm trying to find a simple way to give the xhr object's properties to the car object without manually specifying all 30 options (I know it's doable, but I'm sure there must be a better method, and i'm always up to learn)

Short Story:

How can I use object-x's properties to overwrite object-y's when they have the exact same list of defined properties and both already exist?

Thank you very much in advance!

1 Answer 1

3

Well you would simply

$.extend( car, json );

Where car object is the target and json is the object where the correct values are

Docs http://api.jquery.com/jQuery.extend/

Jsfiddle demo http://jsfiddle.net/qApyZ/

Sign up to request clarification or add additional context in comments.

2 Comments

I think mentioning object.create is worth it.
@Roger I don't see how it could be applied here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.