用值解构声明bug [英] Destructuring declaration bug with the value

查看:122
本文介绍了用值解构声明bug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解解构赋值后的原因, items prop不等于 Gorilla



它将在删除原始对象选项中的主要道具项目:Piggi后使用。我不明白为什么......



'use strict'; let options = {size:100,items:Piggi} let {title =Menu,items:w =Gorilla,size} = options;让a = title;让b = w; console.log(a + - + b); //必须是菜单 - 大猩猩

解决方案

  let {items:w =Gorilla} = options在这里使用初始化的解构声明:

;

语法表示声明一个名为w的变量,其值应该初始化为该属性在由options引用的对象中称为items,或者如果没有这样的属性,则返回字符串Gorilla。

在你的情况中,那么,变量w被初始化为原始对象中items属性的值。



如果您不想从源中获取值对象,则不要:

  let w =Gorilla; 


I can not understand why after destructuring assignment, items prop does not equal Gorilla.

It will be used after deleting the main prop items: "Piggi" in the origin object options. I do not understand why...

    'use strict';
    
    let options = {
      size: 100,
      items: "Piggi"
    }
    
    let { title="Menu", items:w="Gorilla", size } = options;
    
    let a = title;
    let b = w;
    console.log(a + " - " + b);  // must be "Menu - Gorilla"

解决方案

In the destructuring declaration with initialization here:

let { items:w = "Gorilla" } = options;

the syntax means to declare a variable called "w", whose value should be initialized to the value of the property called "items" in the object referenced by "options", or if there is no such property then to the string "Gorilla".

In your case, then, the variable "w" is initialized to the value of the "items" property in the original object.

If you don't want to take the value from the source object, then don't:

let w = "Gorilla";

这篇关于用值解构声明bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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