英文:
Destructuring primitive values in JavaScript
问题
The doc page for destructuring assignment says
> This means if you try to destruct a primitive value, the value will
> get wrapped into the corresponding wrapper object and the property is
> accessed on the wrapper object.
Does this mean the following code?:
const { a, toFixed } = 1;
Is equivalent to:
const { a, toFixed } = {1};
英文:
The doc page for destructuring assignment says
> This means if you try to destruct a primitive value, the value will
> get wrapped into the corresponding wrapper object and the property is
> accessed on the wrapper object.
Does this mean the following code?:
const { a, toFixed } = 1;
Is equivalent to:
const { a, toFixed } = {1};
答案1
得分: 4
No, it means that your 1 is turned into a Number instance, and then the destructuring acts on that.
So it's like
const { a, toFixed } = new Number(1);
The primitive value types number, string, and boolean all have corresponding wrapper types.
英文:
No, it means that your 1 is turned into a Number instance, and then the destructuring acts on that.
So it's like
const { a, toFixed } = new Number(1);
The primitive value types number, string, and boolean all have corresponding wrapper types.
答案2
得分: 2
在JavaScript中,每种原始类型都有对应的对象。
在你的例子中,原始类型是number,因此对应的对象是Number
。
所以,它等同于
const { a, toFixed } = new Number(1);
英文:
Every primitive type in javascript
has its corresponding object.
In your example, the primitive type is number, so the corresponding object is Number
So, it will be equivalent to
const { a, toFixed } = new Number(1);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论