简体   繁体   English

解构数组参数

[英]Destructuring array parameters

I'm learning about destructuring and had a quick query on how to refer to the elements of an array.我正在学习解构并快速查询如何引用数组的元素。

I'm destructuring a nested array within an object in a function's parameter using:我正在使用函数参数中的 object 解构嵌套数组:

function ajaxOptions({
    url,
    headers: [ 
        header0 ="Content-Type: text/plain",
        ...otherHeaders
    ] = [],
    data
} = {}) { 
    //... function body
}

which is giving a default value to the first element, header0 and spreading the other array elements.这是为第一个元素header0提供默认值并传播其他数组元素。

But if I had a settings object:但如果我有一个设置 object:

var settings = {
    url: 'http://someothersite.com', 
    data: 50, 
    callback: sayHello, 
    headers: [,  'Header2', 'Header3'] 
}

and passed it to ajaxOptions(settings) I can't use header0 in the settings object to refer to the array element that's destructured in ajaxOptions can I?并将其传递给ajaxOptions(settings)我不能在设置 object 中使用header0来引用在 ajaxOptions 中解构的数组元素可以吗? In other words I can't use it as a named argument like 'url', 'data' and 'callback' kind of are?换句话说,我不能将它用作像“url”、“数据”和“回调”这样的命名参数吗?

Hope that makes sense.希望这是有道理的。 Here's the complete code if it helps:如果有帮助,这是完整的代码:

function ajaxOptions({
    url: url = "http://www.example.com",
    method: method = "post",
    headers: [ 
        header0 ="Content-Type: text/plain",
        ...otherHeaders
    ] = [],
    data: data,
    callback: callback 

} = {}) {
    return { url, method, headers: [header0, ...otherHeaders], data, callback};
}

function sayHello(){
    console.log('hello');
}


var defaults = ajaxOptions();

var settings = {
    url: 'http://someothersite.com', 
    data: 50, 
    callback: sayHello, 
    headers: [, header0 = 'New Header', 'Header2', 'Header3'] 
}

console.log(ajaxOptions(settings));

Thanks谢谢

I think you should try to solve your problem with objects to reference the array index.我认为您应该尝试使用对象来解决您的问题以引用数组索引。 But if all you really want is to have an array that returns the first values within a function you could try to arrange the arrays as follows.但是,如果您真正想要的只是拥有一个返回 function 中的第一个值的数组,您可以尝试按如下方式排列 arrays。

const defaulHeader = ["Content-Type: text/plain"]
const otherHeaders = ["Some-type: application/json"]

 const headers = [ 
        ...defaulHeader,
        ...otherHeaders
    ]

console.log(headers)

header0 in your parameter list is a parameter, a local variable, just like a is a parameter in:你的参数列表中的header0是一个参数,一个局部变量,就像a是一个参数:

function test(a) { }

And just like you cannot reference a by name outside of that test function, neither can you do that with your code and header0 .就像您不能在该test function 之外引用名称一样,您也不能a代码和header0来做到这一点。 So if you call your function with header0 = , you are actually defining a global variable header0 and assigning it a value on the spot.因此,如果您使用header0 =调用 function ,您实际上是在定义一个全局变量header0并在现场为其分配一个值。 It would produce an error in strict mode.在严格模式下会产生错误。 It has nothing to do with the parameter header0 .它与参数header0

The parameter header0 will get its value from the first element of the array you pass as headers property value.参数header0将从作为headers属性值传递的数组的第一个元素中获取其值。 If the object you pass as argument does not have the headers property, or that property does not define a first array element that is different from undefined , then the local headers0 variable will get the default value.如果您作为参数传递的 object 没有headers属性,或者该属性没有定义不同于undefined的第一个数组元素,则本地headers0变量将获得默认值。

This variable is not to be confused with the property name followed by a colon, that also appears in your parameter section: headers: .请勿将此变量与后跟冒号的属性名称混淆,后者也出现在您的参数部分: headers: This is not a variable, but is merely defining the spot from where variables headers0 and otherHeaders should get their values when the function is called.这不是一个变量,而只是定义了在调用 function 时变量headers0otherHeaders应该从中获取其值的位置。 But headers itself is not a variable.但是headers本身并不是一个变量。

url on the other hand is a variable.另一方面, url一个变量。 It takes its value from the property with the same name.它从具有相同名称的属性中获取其值。 Notice the lack of the colon.注意没有冒号。 That makes it a variable (parameter).这使它成为一个变量(参数)。

The syntax used in destructuring can be quite confusing.解构中使用的语法可能非常混乱。 For instance, if instead of url, you would have url: url2 , then instead of defining variable url , you define variable url2 -- still being the value that the url property has in the object that you pass to the function. For instance, if instead of url, you would have url: url2 , then instead of defining variable url , you define variable url2 -- still being the value that the url property has in the object that you pass to the function.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM