Normally I would set a functions variables with something like this:
var width = null;
var height = null;
if(options) {
if(options.width) width = options.width;
if(options.height) height = options.height;
}
Is there a way to simplify this with something like:
var width = null;
var height = null;
if(options) {
for(var val in options) {
val = options[val];
}
}
In this simple example it doesn’t seem like a big benefit, but in a situation where I have a multitude of variables to set with values from a single object (in such a way that say…ajax does it), it would be much easier to just loop the object and do it that way.
Not directly. If you want to attach things to the global namespace (
window), you could write something like this:…but you would generally want to avoid cluttering up
windowunless absolutely necessary. A commonly used alternative would be to create an object for holding default options, and simply overwrite them as new options are provided: