creating a function that deals with optional parameters

荒凉一梦 提交于 2019-12-24 03:17:08

问题


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:

  1. 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?

  2. Why is there a settings object created? What purpose does this serve?

  3. What is the purpose of the OR statement?

Thanks in advance.


回答1:


  1. 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.

  2. 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.

  3. 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/




回答2:


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 {}




回答3:


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().




回答4:


  1. All options that will be used in options will replace same options from default settings
  2. The setting object create to set all setting in this function to one place
  3. options||{} means if option setted and evaualets to true then return options , return {} overwise. So it is same as if(!options) options={} before this statement;


来源:https://stackoverflow.com/questions/7124960/creating-a-function-that-deals-with-optional-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!