Why does {. . . .0} evaluate to {}?
I just found {....0}
in friend's code. Evaluating it in console returns {}
(empty object).
Why is that? What is the meaning of 4 dots in JavaScript?
javascript spread-syntax number-literal
add a comment |
I just found {....0}
in friend's code. Evaluating it in console returns {}
(empty object).
Why is that? What is the meaning of 4 dots in JavaScript?
javascript spread-syntax number-literal
10
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
Dec 25 '18 at 18:30
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
Dec 26 '18 at 9:38
1
Always relevant
– MikeTheLiar
Dec 26 '18 at 14:36
@JeremyHarris the magic of HNQ
– Pac0
Dec 26 '18 at 21:31
add a comment |
I just found {....0}
in friend's code. Evaluating it in console returns {}
(empty object).
Why is that? What is the meaning of 4 dots in JavaScript?
javascript spread-syntax number-literal
I just found {....0}
in friend's code. Evaluating it in console returns {}
(empty object).
Why is that? What is the meaning of 4 dots in JavaScript?
javascript spread-syntax number-literal
javascript spread-syntax number-literal
edited Dec 25 '18 at 15:57
Mist
asked Dec 25 '18 at 11:37
MistMist
49747
49747
10
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
Dec 25 '18 at 18:30
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
Dec 26 '18 at 9:38
1
Always relevant
– MikeTheLiar
Dec 26 '18 at 14:36
@JeremyHarris the magic of HNQ
– Pac0
Dec 26 '18 at 21:31
add a comment |
10
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
Dec 25 '18 at 18:30
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
Dec 26 '18 at 9:38
1
Always relevant
– MikeTheLiar
Dec 26 '18 at 14:36
@JeremyHarris the magic of HNQ
– Pac0
Dec 26 '18 at 21:31
10
10
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
Dec 25 '18 at 18:30
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
Dec 25 '18 at 18:30
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
Dec 26 '18 at 9:38
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
Dec 26 '18 at 9:38
1
1
Always relevant
– MikeTheLiar
Dec 26 '18 at 14:36
Always relevant
– MikeTheLiar
Dec 26 '18 at 14:36
@JeremyHarris the magic of HNQ
– Pac0
Dec 26 '18 at 21:31
@JeremyHarris the magic of HNQ
– Pac0
Dec 26 '18 at 21:31
add a comment |
4 Answers
4
active
oldest
votes
Four dots actually have no meaning. ...
is the spread operator, and .0
is short for 0.0
.
Spreading 0 (or any number) into an object yields an empty object, therefore {}
.
11
Spreading any number yields an empty object.
– Kresimir
Dec 25 '18 at 11:43
9
Spreading 0 (or any number) yields an empty object
not necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
Dec 25 '18 at 16:37
2
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
Dec 25 '18 at 16:39
2
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
Dec 26 '18 at 0:29
1
@Mist I've updated the answer. I don't think it's needed, but it might be good for clarification. Thanks for the update!
– NikxDa
Dec 26 '18 at 1:08
|
show 2 more comments
Three dots in an object literal are a spread property, e.g.:
const a = { b: 1, c: 1 };
const d = { ...a, e: 1 }; // { b: 1, c: 1, e: 1 }
The last dot with a 0 is a number literal .0
is the same as 0.0
. Therefore this:
{ ...(0.0) }
spreads all properties of the number object into the object, however as numbers don't have any (own) properties you get back an empty object.
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction
(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}
but doesn't work forNumber
(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
Dec 25 '18 at 11:48
3
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
Dec 25 '18 at 11:50
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
Dec 25 '18 at 11:56
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
Dec 25 '18 at 12:06
3
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
Dec 26 '18 at 3:39
|
show 4 more comments
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelifier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
11
new word! i.stack.imgur.com/VyYqA.png
– uhoh
Dec 27 '18 at 0:45
add a comment |
Spread operator {...}
allows iterables to expand. It means that those data types that can be defined in form of key-value
pairs can be expanded. In terms of Object
we call key-value pair as Object property and it's value whereas in terms of arrays
we can think index as key and element in array as it's value.
let obj = { a: 4, b: 1};
let obj2 = { ...obj, c: 2, d: 4}; // {a: 4, b: 1, c: 2, d: 4}
let arr1 = ['1', '2'];
let obj3 = { ...arr1, ...['3']}; // {0: "3", 1: "2"}
In terms of array, as it takes index as key so here it replaces element '1' of arr1
with '3' because both of them have same index in different array.
With strings too spread operator returns non-empty object. As string is an array of character so it treats string as an array.
let obj4 = {...'hi',...'hello'} // {0: "h", 1: "e", 2: "l", 3: "l", 4: "o"}
let obj5 = {...'y',...'x'} // {0: "x" }
But with other primitive data types it return empty object
with Numbers
let obj6 = { ...0.0, ...55} // {}
with Boolean
let obj7 = { ...true, ...false} // {}
In conclusion those data types that can be treated in form of key-value pairs when used with spread operator {...}
returns non-empty object otherwise it returns empty object {}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53922010%2fwhy-does-0-evaluate-to%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Four dots actually have no meaning. ...
is the spread operator, and .0
is short for 0.0
.
Spreading 0 (or any number) into an object yields an empty object, therefore {}
.
11
Spreading any number yields an empty object.
– Kresimir
Dec 25 '18 at 11:43
9
Spreading 0 (or any number) yields an empty object
not necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
Dec 25 '18 at 16:37
2
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
Dec 25 '18 at 16:39
2
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
Dec 26 '18 at 0:29
1
@Mist I've updated the answer. I don't think it's needed, but it might be good for clarification. Thanks for the update!
– NikxDa
Dec 26 '18 at 1:08
|
show 2 more comments
Four dots actually have no meaning. ...
is the spread operator, and .0
is short for 0.0
.
Spreading 0 (or any number) into an object yields an empty object, therefore {}
.
11
Spreading any number yields an empty object.
– Kresimir
Dec 25 '18 at 11:43
9
Spreading 0 (or any number) yields an empty object
not necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
Dec 25 '18 at 16:37
2
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
Dec 25 '18 at 16:39
2
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
Dec 26 '18 at 0:29
1
@Mist I've updated the answer. I don't think it's needed, but it might be good for clarification. Thanks for the update!
– NikxDa
Dec 26 '18 at 1:08
|
show 2 more comments
Four dots actually have no meaning. ...
is the spread operator, and .0
is short for 0.0
.
Spreading 0 (or any number) into an object yields an empty object, therefore {}
.
Four dots actually have no meaning. ...
is the spread operator, and .0
is short for 0.0
.
Spreading 0 (or any number) into an object yields an empty object, therefore {}
.
edited Dec 26 '18 at 1:07
answered Dec 25 '18 at 11:40
NikxDaNikxDa
3,12111737
3,12111737
11
Spreading any number yields an empty object.
– Kresimir
Dec 25 '18 at 11:43
9
Spreading 0 (or any number) yields an empty object
not necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
Dec 25 '18 at 16:37
2
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
Dec 25 '18 at 16:39
2
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
Dec 26 '18 at 0:29
1
@Mist I've updated the answer. I don't think it's needed, but it might be good for clarification. Thanks for the update!
– NikxDa
Dec 26 '18 at 1:08
|
show 2 more comments
11
Spreading any number yields an empty object.
– Kresimir
Dec 25 '18 at 11:43
9
Spreading 0 (or any number) yields an empty object
not necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.
– Hitesh Kumar
Dec 25 '18 at 16:37
2
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
Dec 25 '18 at 16:39
2
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..
– Mist
Dec 26 '18 at 0:29
1
@Mist I've updated the answer. I don't think it's needed, but it might be good for clarification. Thanks for the update!
– NikxDa
Dec 26 '18 at 1:08
11
11
Spreading any number yields an empty object.
– Kresimir
Dec 25 '18 at 11:43
Spreading any number yields an empty object.
– Kresimir
Dec 25 '18 at 11:43
9
9
Spreading 0 (or any number) yields an empty object
not necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.– Hitesh Kumar
Dec 25 '18 at 16:37
Spreading 0 (or any number) yields an empty object
not necessarily if you spread a number at any other places apart from an object, it will throw an error eg [...0] throws an error.– Hitesh Kumar
Dec 25 '18 at 16:37
2
2
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
Dec 25 '18 at 16:39
@HiteshKumar Spreading non-iterable objects inside an array will indeed throw an error, but that has nothing to do with this question. I am referring to the object-spread mentioned. :)
– NikxDa
Dec 25 '18 at 16:39
2
2
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.
Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..– Mist
Dec 26 '18 at 0:29
NikxDa I think that @HiteshKumar made an important point. It's better to be more explicit about cases where your statements holds true.
Spreading 0 (or any number) in object literal yields an empty object
Contains more useful information..– Mist
Dec 26 '18 at 0:29
1
1
@Mist I've updated the answer. I don't think it's needed, but it might be good for clarification. Thanks for the update!
– NikxDa
Dec 26 '18 at 1:08
@Mist I've updated the answer. I don't think it's needed, but it might be good for clarification. Thanks for the update!
– NikxDa
Dec 26 '18 at 1:08
|
show 2 more comments
Three dots in an object literal are a spread property, e.g.:
const a = { b: 1, c: 1 };
const d = { ...a, e: 1 }; // { b: 1, c: 1, e: 1 }
The last dot with a 0 is a number literal .0
is the same as 0.0
. Therefore this:
{ ...(0.0) }
spreads all properties of the number object into the object, however as numbers don't have any (own) properties you get back an empty object.
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction
(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}
but doesn't work forNumber
(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
Dec 25 '18 at 11:48
3
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
Dec 25 '18 at 11:50
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
Dec 25 '18 at 11:56
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
Dec 25 '18 at 12:06
3
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
Dec 26 '18 at 3:39
|
show 4 more comments
Three dots in an object literal are a spread property, e.g.:
const a = { b: 1, c: 1 };
const d = { ...a, e: 1 }; // { b: 1, c: 1, e: 1 }
The last dot with a 0 is a number literal .0
is the same as 0.0
. Therefore this:
{ ...(0.0) }
spreads all properties of the number object into the object, however as numbers don't have any (own) properties you get back an empty object.
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction
(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}
but doesn't work forNumber
(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
Dec 25 '18 at 11:48
3
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
Dec 25 '18 at 11:50
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
Dec 25 '18 at 11:56
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
Dec 25 '18 at 12:06
3
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
Dec 26 '18 at 3:39
|
show 4 more comments
Three dots in an object literal are a spread property, e.g.:
const a = { b: 1, c: 1 };
const d = { ...a, e: 1 }; // { b: 1, c: 1, e: 1 }
The last dot with a 0 is a number literal .0
is the same as 0.0
. Therefore this:
{ ...(0.0) }
spreads all properties of the number object into the object, however as numbers don't have any (own) properties you get back an empty object.
Three dots in an object literal are a spread property, e.g.:
const a = { b: 1, c: 1 };
const d = { ...a, e: 1 }; // { b: 1, c: 1, e: 1 }
The last dot with a 0 is a number literal .0
is the same as 0.0
. Therefore this:
{ ...(0.0) }
spreads all properties of the number object into the object, however as numbers don't have any (own) properties you get back an empty object.
edited Dec 26 '18 at 19:59
answered Dec 25 '18 at 11:42
Jonas WilmsJonas Wilms
57.6k43051
57.6k43051
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction
(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}
but doesn't work forNumber
(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
Dec 25 '18 at 11:48
3
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
Dec 25 '18 at 11:50
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
Dec 25 '18 at 11:56
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
Dec 25 '18 at 12:06
3
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
Dec 26 '18 at 3:39
|
show 4 more comments
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works forFunction
(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}
but doesn't work forNumber
(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
Dec 25 '18 at 11:48
3
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Thereforex.k
will get lost.
– Jonas Wilms
Dec 25 '18 at 11:50
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
Dec 25 '18 at 11:56
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
Dec 25 '18 at 12:06
3
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
Dec 26 '18 at 3:39
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works for
Function
(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}
but doesn't work for Number
(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
Dec 25 '18 at 11:48
You lead me to thinking - I can spread any variable, and own keys will be spread into the new object? It works for
Function
(function x() {}), (x.k = 'v'), ({...x})// {k: 'v'}
but doesn't work for Number
(x = 10), (x.k = 'v'), ({...x}) // {}
– Mist
Dec 25 '18 at 11:48
3
3
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Therefore
x.k
will get lost.– Jonas Wilms
Dec 25 '18 at 11:50
@mist because numbers (and other primitives) get "boxed" into objects when you work with them as objects, and "unboxed" directly afterwards. Therefore
x.k
will get lost.– Jonas Wilms
Dec 25 '18 at 11:50
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
Dec 25 '18 at 11:56
What does 'boxed' means exactly? E.g. when I used the dot operator (property) I worked with the number as object. If I am correct, that's just one case. Are there other cases when 'boxing' is happening? Does it apply only to numbers? Is there a perf reason or something? I guess this is for other question, and I should study it further. Could you point me to some book or something?
– Mist
Dec 25 '18 at 11:56
1
1
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
Dec 25 '18 at 12:06
Thanks! I see why my key on number couldn't work. Yayy boxing!
– Mist
Dec 25 '18 at 12:06
3
3
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
Dec 26 '18 at 3:39
Numbers don't have any own enumerable properties. But they do have properties.
– Patrick Roberts
Dec 26 '18 at 3:39
|
show 4 more comments
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelifier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
11
new word! i.stack.imgur.com/VyYqA.png
– uhoh
Dec 27 '18 at 0:45
add a comment |
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelifier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
11
new word! i.stack.imgur.com/VyYqA.png
– uhoh
Dec 27 '18 at 0:45
add a comment |
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelifier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
In a simple terms {...}
spread operator in javascript extends one object/array with another.
So, when babelifier tries extending one with another, it has to identify whether it is trying to extend an array or an object.
In the case of array
, it iterates over elements.
In the case of object
, it iterates over keys.
In this scenario, the babelyfier is trying to extract keys for number
by checking the Object's own property call
which is missing for number
so it returns empty Object.
edited Dec 26 '18 at 18:02
answered Dec 26 '18 at 11:51
Rajendra kumar VankadariRajendra kumar Vankadari
1,207912
1,207912
11
new word! i.stack.imgur.com/VyYqA.png
– uhoh
Dec 27 '18 at 0:45
add a comment |
11
new word! i.stack.imgur.com/VyYqA.png
– uhoh
Dec 27 '18 at 0:45
11
11
new word! i.stack.imgur.com/VyYqA.png
– uhoh
Dec 27 '18 at 0:45
new word! i.stack.imgur.com/VyYqA.png
– uhoh
Dec 27 '18 at 0:45
add a comment |
Spread operator {...}
allows iterables to expand. It means that those data types that can be defined in form of key-value
pairs can be expanded. In terms of Object
we call key-value pair as Object property and it's value whereas in terms of arrays
we can think index as key and element in array as it's value.
let obj = { a: 4, b: 1};
let obj2 = { ...obj, c: 2, d: 4}; // {a: 4, b: 1, c: 2, d: 4}
let arr1 = ['1', '2'];
let obj3 = { ...arr1, ...['3']}; // {0: "3", 1: "2"}
In terms of array, as it takes index as key so here it replaces element '1' of arr1
with '3' because both of them have same index in different array.
With strings too spread operator returns non-empty object. As string is an array of character so it treats string as an array.
let obj4 = {...'hi',...'hello'} // {0: "h", 1: "e", 2: "l", 3: "l", 4: "o"}
let obj5 = {...'y',...'x'} // {0: "x" }
But with other primitive data types it return empty object
with Numbers
let obj6 = { ...0.0, ...55} // {}
with Boolean
let obj7 = { ...true, ...false} // {}
In conclusion those data types that can be treated in form of key-value pairs when used with spread operator {...}
returns non-empty object otherwise it returns empty object {}
add a comment |
Spread operator {...}
allows iterables to expand. It means that those data types that can be defined in form of key-value
pairs can be expanded. In terms of Object
we call key-value pair as Object property and it's value whereas in terms of arrays
we can think index as key and element in array as it's value.
let obj = { a: 4, b: 1};
let obj2 = { ...obj, c: 2, d: 4}; // {a: 4, b: 1, c: 2, d: 4}
let arr1 = ['1', '2'];
let obj3 = { ...arr1, ...['3']}; // {0: "3", 1: "2"}
In terms of array, as it takes index as key so here it replaces element '1' of arr1
with '3' because both of them have same index in different array.
With strings too spread operator returns non-empty object. As string is an array of character so it treats string as an array.
let obj4 = {...'hi',...'hello'} // {0: "h", 1: "e", 2: "l", 3: "l", 4: "o"}
let obj5 = {...'y',...'x'} // {0: "x" }
But with other primitive data types it return empty object
with Numbers
let obj6 = { ...0.0, ...55} // {}
with Boolean
let obj7 = { ...true, ...false} // {}
In conclusion those data types that can be treated in form of key-value pairs when used with spread operator {...}
returns non-empty object otherwise it returns empty object {}
add a comment |
Spread operator {...}
allows iterables to expand. It means that those data types that can be defined in form of key-value
pairs can be expanded. In terms of Object
we call key-value pair as Object property and it's value whereas in terms of arrays
we can think index as key and element in array as it's value.
let obj = { a: 4, b: 1};
let obj2 = { ...obj, c: 2, d: 4}; // {a: 4, b: 1, c: 2, d: 4}
let arr1 = ['1', '2'];
let obj3 = { ...arr1, ...['3']}; // {0: "3", 1: "2"}
In terms of array, as it takes index as key so here it replaces element '1' of arr1
with '3' because both of them have same index in different array.
With strings too spread operator returns non-empty object. As string is an array of character so it treats string as an array.
let obj4 = {...'hi',...'hello'} // {0: "h", 1: "e", 2: "l", 3: "l", 4: "o"}
let obj5 = {...'y',...'x'} // {0: "x" }
But with other primitive data types it return empty object
with Numbers
let obj6 = { ...0.0, ...55} // {}
with Boolean
let obj7 = { ...true, ...false} // {}
In conclusion those data types that can be treated in form of key-value pairs when used with spread operator {...}
returns non-empty object otherwise it returns empty object {}
Spread operator {...}
allows iterables to expand. It means that those data types that can be defined in form of key-value
pairs can be expanded. In terms of Object
we call key-value pair as Object property and it's value whereas in terms of arrays
we can think index as key and element in array as it's value.
let obj = { a: 4, b: 1};
let obj2 = { ...obj, c: 2, d: 4}; // {a: 4, b: 1, c: 2, d: 4}
let arr1 = ['1', '2'];
let obj3 = { ...arr1, ...['3']}; // {0: "3", 1: "2"}
In terms of array, as it takes index as key so here it replaces element '1' of arr1
with '3' because both of them have same index in different array.
With strings too spread operator returns non-empty object. As string is an array of character so it treats string as an array.
let obj4 = {...'hi',...'hello'} // {0: "h", 1: "e", 2: "l", 3: "l", 4: "o"}
let obj5 = {...'y',...'x'} // {0: "x" }
But with other primitive data types it return empty object
with Numbers
let obj6 = { ...0.0, ...55} // {}
with Boolean
let obj7 = { ...true, ...false} // {}
In conclusion those data types that can be treated in form of key-value pairs when used with spread operator {...}
returns non-empty object otherwise it returns empty object {}
answered Jan 5 at 5:25
JS EngineJS Engine
666313
666313
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53922010%2fwhy-does-0-evaluate-to%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
10
Viewed almost 2500 times in 6 hours? It appears your friend is using the spread operator in a different context.
– Jeremy Harris
Dec 25 '18 at 18:30
This is more of a "how this expression is parsed" question. Type this in JS console and you'll notice that the 4th dot is colored differently... same color as zero.
– Salman A
Dec 26 '18 at 9:38
1
Always relevant
– MikeTheLiar
Dec 26 '18 at 14:36
@JeremyHarris the magic of HNQ
– Pac0
Dec 26 '18 at 21:31