I am storing css values in an object for use in two different scenarios:
styles = {
on: { left:10, top:20 },
off: { left:0, top:30 }
};
The first way I use this is for a simple mouseover effect:
$nav.hover(
function() {
$(this).css(styles.on);
},
function() {
$(this).css(styles.off);
}
);
However, for my other use I am doing an animation and I need extra (non-css) properties like ease
and onComplete
. Ideally I would like to be able to duplicate the styles
object and append my new properties to it like this:
var anim = styles;
anim.ease = Power3.easeInOut;
anim.onComplete = function() {/*stuff*/};
This will work fine for the second use but unfortunately due to storing a reference-by-value this also adds the new properties to the original styles
variable as well, which in turn will cause the .css()
method to try and assign ease
etc as a css value.
How can I quickly clone the source object to add in the new properties without affecting the original?
I am aware of the arguments surrounding whether Javascript can pass by reference (Is there thing like pass by value pass by reference in JavaScript?, Is JavaScript a pass-by-reference or pass-by-value language?) but I don't want to get into a discussion on that...