When I am trying out some object destruction syntax in some browser consoles, something unexpected happened. Firstly I entered
action = {
type: "SET_APPS_UI_REVERT",
device: 23456,
managedApps: "12345"
}
and then
( { type, status, appsInfo, device,managedApps,appName } = action);
lastly
status
So both chrome and firefox decided to give me an "undefined"
that is a string, rather than a undefined value, while edge would give me an usual undefined
. However, when I typed
const { type, status, appsInfo, device,managedApps,appName } = action
and then
status
in edge, it gives me a ""
rather than undefined
.
Is this results of some browser inconsistency? Or actually some bugs?
Screenshots are below
chrome-66-0-3359-181-no-const.PNG
edge-41-16299-402-0-with-edgehtml-16-16299-no-const.PNG
Using the following syntax:
You are explicitly saying "destructure
action.status
to the existing variablestatus
.If you don't already have a variable called
status
in the local scope then it will attempt to assignaction.status
to the window.status property. This property only accepts a string, so whenaction
does not have astatus
property, you've effectively done this:Since
window.status
coerces to a string, when this is read back you getwindow.status === "undefined";
.Even though this doesn't have any effect on the statusbar in Firefox, it still exhibits the behaviour.
Your second part is expected behaviour also:
Is different from before because you're declaring a scoped variable called
status
. This is where the browser differences come in. In Chrome, when you declareconst status
in devtools, the devtools is considered the "scope" and you can access it after it's declared. In Edge however, you can declareconst
within Dev Tools, but you can never access the value. Reference.So in Edge, you get this kind of behaviour: