Comma in front of square bracket - MDN docs

前端 未结 3 1250
耶瑟儿~
耶瑟儿~ 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.

提交回复
热议问题