解构数组如何获得长度属性 [英] how does destructuring array get length property

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

问题描述

我在一篇文章中遇到了这个解构表达式.

I came across this destructuring expression in an article.

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

length 如何得到 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)

现在有意义吗?

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

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