function extend(destination, source){
for(prop in source){
destination[prop] = source[prop];
}
return destination;
}
Prototype's extend method is here: http://prototypejs.org/api/object/extend
jQuery's extend method is here: http://docs.jquery.com/Core/jQuery.extend#object
function f(){
var options = extend({
width: 200,
height: 500,
title: 'Default Title'
}, arguments[0] || {});
return options;
}
| assertEquals(f().width, 200, "default width should be 200") | |
| assertEquals(f().height, 500, "default height should be 500") | |
| assertEquals(f().title, "Default Title", "default title should be 'Default Title'") | |
| assertEquals(f({width:577}).width, 577, "new width should be 577") | |
| assertEquals(f({width:577}).height, 500, "default height is still 500") | |
| assertEquals(f({title:'This is my new title'}).title, "This is my new title", "new title should be 'This is my new title'") |