当我在某些浏览器控制台中尝试一些对象破坏语法时,发生了意外情况。 首先我进入了
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
。
这是某些浏览器不一致的结果吗?或者实际上是一些错误?
屏幕截图位于
之下边缘41-16299-402-0与 - edgehtml-16-16299-NO-const.PNG
答案 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