Comma in front of square bracket - MDN docs

前端 未结 3 1244
耶瑟儿~
耶瑟儿~ 2021-01-27 01:53

MDN is my primary Javascript resource. I often see a notation like ( currentValue[, index[, array]]) as in:

l         


        
相关标签:
3条回答
  • 2021-01-27 02:37

    It is an invalid JS syntax, and it would throw an error.

    However, it is used to denote optional arguments as a pseudo-code.

    The parts in the square brackets are considered optional (and in the real code the brackets should be omitted), so, you might call this function like:

    let new_array = arr.map(function callback(currentValue) {
        // return element for new_array
    })
    //or
    let new_array = arr.map(function callback(currentValue) {
        // return element for new_array
    }, thisArg)
    //or
    let new_array = arr.map(function callback(currentValue, index) {
        // return element for new_array
    })
    //or
    let new_array = arr.map(function callback(currentValue, index) {
        // return element for new_array
    }, thisArg)
    //or
    let new_array = arr.map(function callback(currentValue, index, array) {
        // return element for new_array
    })
    //or
    let new_array = arr.map(function callback(currentValue, index, array) {
        // return element for new_array
    }, thisArg)
    

    and each of them is valid.

    0 讨论(0)
  • 2021-01-27 02:40

    That means the callback requires a currentValue as the first argument, and index and array are optional arguments.

    0 讨论(0)
  • 2021-01-27 02:43

    The square brackets mean that the enclosed parameter is optional.

    Refer MDN syntax section for more info.

    0 讨论(0)
提交回复
热议问题