destructuring数组如何获取length属性 [英] how does destructuring array get length property

查看:109
本文介绍了destructuring数组如何获取length属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一篇文章中遇到了这种解构表达。

I came across this destructuring expression in an article.

const words = ['oops', 'gasp', 'shout', 'sun'];
let { length } = words;
console.log(length); // 4

长度如何获得价值4?我知道 .length 是数组的属性,但这种语法是如何工作的?它似乎在做 let length = words.length; ,实际上在babel中确实输出了它。但我的问题是它背后的逻辑是什么?
令我困惑的是一系列值的混合以及 {length} 的使用。

How does length get the value of 4? I know .length is a property of the array, but how does this syntax work? It seems to be doing let length = words.length; and in fact in babel does output it as such. But my question is what is the logic behind it? What is confusing me is the mix of an array of values and the the use of {length}.

我已阅读 MDN 的说明,但无法看到这个例子的解释。

I have read MDN 's description but can't see this example explained.

推荐答案

认为代码是

const words = {0:'oops', 1:'gasp', 2:'shout', 3:'sun', length:4};
let { length } = words;
console.log(length);

它本质上是(永远不要告诉所有其他数组附带的东西)

Which it essentially is (nevermind all the other stuff arrays come with)

现在有意义吗?

这篇关于destructuring数组如何获取length属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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