I found this code in Jquery in Action:
function complex(p1,options) {
var settings = $.extend({
options1: devaultVal1,
options2: devaultVal2,
options3: devaultVal3,
options4: devaultVal4,
options5: devaultVal5,
},options||{});
//remainder of function definition..
};
I don't understand three things about this pattern:
The $.extend()
function merges the options
parameter with the first parameter object. If I were to pass an 'optional' parameter when invoking complex()
, how would it tie into this object?
Why is there a settings
object created? What purpose does this serve?
What is the purpose of the OR statement?
Thanks in advance.
The purpose of the options specified are to give you default values. If you specify one of these, then the value you provide will override what is there by default.
settings is the object that will be used throughout the function. It could just as easily be called options and would override the value of options passed through to the function.
The or is there to prevent an error if options comes through as null. It says use options, if it is null, use an empty object literal.
Let's look at some examples:
function complex(p1,options) {
var settings = $.extend({
option1: "val1",
option2: "val2"
},options||{});
console.log(settings);
};
complex("P1 Value"); //settings = {option1: "val1", option2: "val2"}
complex("P1 Value", {option1: "newval"}); //settings = {option1: "newval", option2: "val2"}
complex("P1 Value", {option1: "newval", option2: "newval"}); //settings = {option1: "newval", option2: "newval"}
complex("P1 Value", {option3: "Third?!"}); //settings = {option1: "newval", option2: "newval", option3: "Third?!"}
Here is a jsfiddle that logs out the values. http://jsfiddle.net/8G9PJ/
a) $.extend basically means if you pass in a json object that overrides the default options set those values for the properties. if you pass in {options1: "1", options3: "3"} then you are settings will have those two paramters overriden by the values that you passed
b) settings is the final settings object that wil be used by the library. so yor library will basically access settings.options1 settings.options2 wherever needs and nt defaultVal1, defaultVal2 if they are properties.
c) if there is no options set i.e options is undefined then dnt reset the default values. basically the way the extend function works is it loops through the default proprty names checks if the options json has those properties if yes then reset it else dont. now if you pass in undefined this will fail hence {}
2) The settings
object is created to store all the settings for your function. This provides all the defaults, but can be overridden by passing an object as the second parameter to your complex()
function.
1) Any keys found in your optional options
parameter object, will override that key in the default settings. That way, you can pass an object containing only the settings you want to alter.
3) The or statement ascertains that the second parameter passed to $.extend()
is an object. If options
is undefined (because you didn't pass it in), it'll still send an object to $.extend()
.