使用对象销毁

时间:2018-05-29 08:00:57

标签: javascript google-chrome firefox ecmascript-6 object-destruction

当我在某些浏览器控制台中尝试一些对象破坏语法时,发生了意外情况。 首先我进入了

action = {
      type: "SET_APPS_UI_REVERT",
      device: 23456,
      managedApps: "12345"
    }

然后

( { type, status, appsInfo, device,managedApps,appName } = action);

最后

status

所以chrome和firefox都决定给我一个"undefined"这是一个字符串,而不是一个未定义的值,而edge会给我一个通常的undefined。但是,当我输入

const { type, status, appsInfo, device,managedApps,appName } = action

然后

status

在边缘,它为我提供""而不是undefined

这是某些浏览器不一致的结果吗?或者实际上是一些错误?

屏幕截图位于

之下

铬66-0-3359-181-NO-const.PNG chrome-66-0-3359-181-no-const.PNG

边缘41-16299-402-0与 - edgehtml-16-16299-NO-const.PNG edge-41-16299-402-0-with-edgehtml-16-16299-no-const.PNG

边缘41-16299-402-0与 - edgehtml-16-16299与 - const.PNG edge-41-16299-402-0-with-edgehtml-16-16299-with-const.PNG

火狐-60-0-1-NO-const.PNG firefox-60-0-1-no-const.PNG

1 个答案:

答案 0 :(得分:0)

使用以下语法:

( { type, status, appsInfo, device,managedApps,appName } = action);

您明确说“将现有变量action.status解构为status

如果您在本地范围内还没有名为status的变量,那么它将尝试将action.status分配给window.status属性。此属性只接受字符串,因此当action没有status属性时,您已经有效地完成了此操作:

window.status = undefined;

由于window.status强制转换为字符串,因此在回读时会收到window.status === "undefined";

即使Firefox中的状态栏上没有任何效果,它仍会表现出这种行为。

你的第二部分也是预期的行为:

const { type, status, appsInfo, device,managedApps,appName } = action

与之前不同,因为您声明了一个名为status范围变量。这就是浏览器差异的来源。在Chrome中,当您在devtools中声明const status时,devtools被视为“范围”,您可以在声明后访问它。但是,在Edge中,您可以在Dev Tools中声明const,但您永远无法访问该值。 Reference

所以在Edge中,你会遇到这种行为:

{
    const status = action.status;
    typeof status === "undefined"; // true
}
status; // The value of window.status. const status is out of scope