JavaScript中可选参数的文档 [英] Documentation for optional parameters in JavaScript

查看:162
本文介绍了JavaScript中可选参数的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的文档addEventListener 我看到以下模式:

From the documentation for addEventListener I see the following pattern:

target.addEventListener(type, listener[, useCapture]);

现在我明白 useCapture 是一个可选的参数。为什么 [然后在逗号之前开始(),而不是在逗号跟随监听器参数?除了 useCapture 是可选的事实外,附件对 [] 我也看过jQuery文档中类似的文档模式,例如 on()方法文档

Now I understand that useCapture is an optional parameter. Why does the [ then start before the comma (,) and not immediately after the comma that follows the listener parameter? What does the enclosing pair of [] actually suggest apart from the fact that useCapture is optional? I have also seen similar documentation patterns in jQuery documentation, e.g. the on () method documentation.

.on( events [, selector ] [, data ], handler(eventObject) )


推荐答案

方括号表示它们内的东西是可选的–要么你有它,要么你没有列出有效的调用表单的简明方法。

Square brackets mean the thing inside them is optional – either you have it or you don't. It is a concise way of listing the valid invocation forms.

目标。 addEventListener(type,listener [,useCapture]);

有两种有效的格式:

target.addEventListener(type, listener            ); // without
target.addEventListener(type, listener, useCapture); // with

如果逗号在方括号外,两个表单将是

If the comma was outside the square brackets the two forms would be

target.addEventListener(type, listener,           ); // without (syntax error)
target.addEventListener(type, listener, useCapture); // with



jQuery示例



.on(events [,selector] [,data],handler);

这个有点棘手。选择器和数据是可选的,因此有四种有效的形式:

This one is a bit tricky. The selector and data are optional so there are four valid forms:

.on( events                , handler ); // without both
.on( events          , data, handler ); // without selector, with data
.on( events, selector      , handler ); // with selector, without data
.on( events, selector, data, handler ); // with both

问题是第二和第三个表单都有三个参数,所以不是明白如何解释论据。看来,jQuery根据中间参数的类型决定:如果它是一个字符串,则选择第三种形式;

The problem is that the second and third forms both have three parameters, so it's not obvious how the arguments will be interpreted. It appears that jQuery decides based on the type of the middle argument: if it's a string the third form is chosen; otherwise the second form is chosen.

因此,以下内容将以hi作为选择器,没有任何内容数据参数:

So the following will have "hi" as the selector and nothing as the data argument:

.on( events          , "hi", handler ); // "hi" is the selector (!)

强制jQuery使用hi作为数据参数,必须为选择器指定 null

To force jQuery to use "hi" as the data argument, null must be given for the selector:

.on( events, null    , "hi", handler ); // "hi" is the data argument

这是明确的第四个表单,文档说一个 null 选择器被视为相同的省略选择器。

This is unambiguously the fourth form, and the docs say that a null selector is treated the same an omitted selector.

在文档中,您经常会看到嵌套的方括号。以下是Unix命令 man 的文档中的简化示例:

In documentation you'll often see nested square brackets. Here is a simplified example from the documentation for the Unix command man:

man [--warnings [= type]] page

这意味着以下表单有效:

This means the following forms are valid:

man                   javac    # without outer
man --warnings        javac    # with outer (without inner)
man --warnings=number javac    # with outer (with inner)

但以下内容无效:

man           =number javac    # is this with or without outer?

这篇关于JavaScript中可选参数的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆