创建一个处理可选参数的函数
我在 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..
};
我不明白有关此模式的三件事:
$.extend()
函数将options
参数与第一个参数对象。如果我在调用complex()
时传递一个“可选”参数,它将如何绑定到该对象?为什么要创建一个
settings
对象?这是什么目的?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:
The
$.extend()
function merges theoptions
parameter with the first parameter object. If I were to pass an 'optional' parameter when invokingcomplex()
, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
指定选项的目的是为您提供默认值。如果您指定其中之一,那么您提供的值将覆盖默认值。
settings 是将在整个函数中使用的对象。它可以很容易地被称为选项,并且会覆盖传递给函数的选项值。
or 的作用是防止选项为 null 时出现错误。它说使用选项,如果为空,则使用空对象文字。
让我们看一些示例:
这是一个打印值的 jsfiddle。 http://jsfiddle.net/8G9PJ/
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:
Here is a jsfiddle that logs out the values. http://jsfiddle.net/8G9PJ/
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 {}
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 yourcomplex()
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. Ifoptions
is undefined (because you didn't pass it in), it'll still send an object to$.extend()
.options
中使用的所有选项都将替换默认设置中的相同选项setting
对象创建将此函数中的所有设置设置到一个位置options||{ }
表示如果设置了 option 且 evaualets 为 true,则返回options
,否则返回 {} 。所以与该语句之前的if(!options) options={}
相同;options
will replace same options from default settingssetting
object create to set all setting in this function to one placeoptions||{}
means if option setted and evaualets to true then returnoptions
, return {} overwise. So it is same asif(!options) options={}
before this statement;