每个参数之间的MDN javascript语法文档中的括号是什么意思? [英] What does bracket mean in MDN javascript syntax document between each parameters?

查看:53
本文介绍了每个参数之间的MDN javascript语法文档中的括号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白MDN javascript语法文档中所有这些括号的含义.

例如,

解决方案

这些括号表示可选参数.这意味着,您不必包括那些参数.

示例:假设我具有递增功能: inc(x)其中需要一个参数.调用时,它将使该变量精确地加一. inc(x)等于 x ++ ,但是我也想有一个 option 来增加任意数量.例如: inx(x,3)给出与 x = x + 3 相同的结果.然后,我可以像在MDN上这样描述我的功能:

  function inc(变量[,increment]) 

I do not understand what all these brackets mean in MDN javascript syntax document.

For example, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

in this document, there are brackets "[" between each parameter before the comma ','

Array.prototype.map()

var new_array = arr.map(function callback(currentValue[, index[, array]]) {
    // Return element for new_array
}[, thisArg])

but when i actually code, for example,

var numbers = [1, 4, 9];
var info = numbers.map(function(num,index) { 
    return "index: " + index + ", number: " + num;
});

console.log(info)

//output:   ["index: 0, number: 1", "index: 1, number: 4", "index: 2, number: 9"]

I do not use array or put [ in the parameters... So what does these brackets mean in the document??

解决方案

These brackets mean optional parameters. It means, you don't have to include those parameters.

Example: Lets say I have function for incrementing: inc(x) Which needs one parameter. When called, it will increment that variable exactly by one. inc(x) equals x++ But I would like to have an option to increment by any number as well. Eg.: inx(x, 3) which gives the same result as x = x + 3. Then I can describe my function similarly as on MDN as:

function inc(variable [,increment])

这篇关于每个参数之间的MDN javascript语法文档中的括号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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