This is similar to Object.extend, but you get back the original
object and keys aren't overwritten:
Object.applyDefaults = function(obj, defaults) {
obj= obj||{};
if (!defaults)
return obj;
for (var p in defaults) {
if (p in obj)
continue;
obj[p]= defaults[p];
}
return obj;
}
I use this all the time to accept a config hash and fill in default
values. This allows me to define my code a bit more modularly:
function MyClass(config) {
this.config= Object.applyDefaults(config, arguments.callee.DEFAULTS);
}
MyClass.DEFAULTS = {
...
};
I know the prototype way is to use Object.extend, but this allows a
greater modularity of code. For example, I can use the same defaults
multiple times.
--
Jeff Watkins
UI Engineer, Online Apple Store
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---