创建一个处理可选参数的函数

发布于 2024-11-30 17:34:49 字数 681 浏览 0 评论 0原文

我在 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..
        };

我不明白有关此模式的三件事:

  1. $.extend() 函数将 options 参数与第一个参数对象。如果我在调用 complex() 时传递一个“可选”参数,它将如何绑定到该对象?

  2. 为什么要创建一个settings对象?这是什么目的?

  3. OR 语句的目的是什么?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深爱成瘾 2024-12-07 17:34:49
  1. 指定选项的目的是为您提供默认值。如果您指定其中之一,那么您提供的值将覆盖默认值。

  2. settings 是将在整个函数中使用的对象。它可以很容易地被称为选项,并且会覆盖传递给函数的选项值。

  3. or 的作用是防止选项为 null 时出现错误。它说使用选项,如果为空,则使用空对象文字。

让我们看一些示例:

  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?!"}

这是一个打印值的 jsfiddle。 http://jsfiddle.net/8G9PJ/

  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/

烧了回忆取暖 2024-12-07 17:34:49

a) $.extend 基本上意味着如果您传入一个覆盖默认选项的 json 对象,则为属性设置这些值。如果您传入 {options1: "1", options3: "3"} 那么您的设置将被您传递的值覆盖这两个参数

b) settings 是将由库使用的最终设置对象。所以你的库基本上会在需要的地方访问settings.options1和settings.options2,如果它们是属性,则不会访问defaultVal1、defaultVal2。

c) 如果没有设置选项,即选项未定义,则不重置默认值。基本上,扩展函数的工作方式是循环遍历默认属性名称,检查选项 json 是否具有这些属性,如果是,则重置它,否则不。现在,如果您传入未定义的值,则会失败,因此 {}

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

巷子口的你 2024-12-07 17:34:49

2) 创建 settings 对象来存储函数的所有设置。这提供了所有默认值,但可以通过将对象作为第二个参数传递给您的 complex() 函数来覆盖。

1) 在可选 options 参数对象中找到的任何键都将覆盖默认设置中的该键。这样,您可以传递一个仅包含您想要更改的设置的对象。

3) or 语句确定传递给$.extend() 的第二个参数是一个对象。如果 options 未定义(因为您没有传入它),它仍然会向 $.extend() 发送一个对象。

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

谢绝鈎搭 2024-12-07 17:34:49
  1. options 中使用的所有选项都将替换默认设置中的相同选项
  2. setting 对象创建将此函数中的所有设置设置到一个位置
  3. options||{ } 表示如果设置了 option 且 evaualets 为 true,则返回 options ,否则返回 {} 。所以与该语句之前的 if(!options) options={} 相同;
  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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文