在JavaScript中解构原始值

huangapple go评论71阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2023年3月9日 22:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定