Unexpected behavior of JavaScript's Set object [duplicate] - javascript

This question already has answers here:
Simplest way to merge ES6 Maps/Sets?
(16 answers)
Closed 3 years ago.
Just spotted strange behavior of JS's Set object and decided to ask stackoverflow community :) So that's the problem:
Screen from console:
Code for copy-paste:
let a = new Set([1])
let b = new Set([2, 3])
a.add(...b) // result is set of {1, 2}
Problem is that set a does not contain value 3 after add function call.
This behavior have an explanation or it's more like a bug?
Thanks in advance

You can add create a new set like this:
let a = new Set([1])
let b = new Set([2, 3])
const c = new Set([...a, ...b])
Variable c contains all the items that you need.
The problem is that the add method of the set get only one parameter, no a arguments, if for that reason that only add one value.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/add

If you do this:
let a = new Set([1])
a.add(2,3);
a will still only have 1,2 as elements. The spread operator is basically doing the same as above

Related

How to copy one object to another variable without passing refrence in javascript [duplicate]

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 3 years ago.
I have one object
var a = {b:{c:{d:{e:1}}}}
I want to copy that object to another variable b, without passing a reference, so I try spread operator
var b = {...a}
It is not removing reference at a deep level, as I change the value in the object "a" it also changes the value in the object "b"
a.b.c.d.e = 2
console.log(b.b.c.d.e)
it give output 2 instead of 1
var a = {b:{c:{d:{e:1}}}};
var b = {...a};
a.b.c.d.e = 2;
console.log(b.b.c.d.e)
document.write(b.b.c.d.e)
How can I handle this issue
I've always done this to copy JSON objects. Works great IMO there may be a better way out there.
var a = {b:{c:{d:{e:1}}}};
var b = JSON.parse(JSON.stringify(a));
a.b.c.d.e = 2;
console.log(b.b.c.d.e)
document.write(b.b.c.d.e)

programmatically creating classes undefined [duplicate]

This question already has answers here:
javascript if giving "undefined" in console [duplicate]
(2 answers)
Closed 3 years ago.
I am following a book on Javascript and the example doesn't seem to return any value. I don't have much experience with OOP though.
I believe this is a programmatical way to create classes, but it returns undefined any way
What do you make of this?
Thank you in advance
This is expected, variable declarations always return undefined.
E.g., let a = 0, var b = 0, const c = 0.
This doesn't mean a, b, and c are undefined though.
Likewise for your example, createClass and Book aren't undefined, as seen by your console.log.

I just started my journey in Javascript, am finding this array question bit tricky and confusing. Please someone should help me out here [duplicate]

This question already has answers here:
why javascript const keyword is not working in array?
(3 answers)
Closed 3 years ago.
const numbers = [1,2,3,4,5]
let luckyNum = numbers.pop()
What will be the value of numbers?
Hint: numbers is stored in a constant not a variable
From the MDN you can read:
Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through reassignment, and it can't be redeclared.
In you particular case the numbers variable holds a reference to the array and that reference will remain "constant", not the array itself. As you can check on next example:
const numbers = [1,2,3,4,5];
let luckyNum = numbers.pop();
console.log("luckyNum:", luckyNum, "numbers:", numbers);
// Now, next line will trhow an error, because we are
// trying to do a reassingment on a const variable:
numbers = [];
In the particular case you are interested, you can use Object.freeze() to prohibit changes on an array of primitives values:
const numbers = [1, 2, 3, 4, 5];
Object.freeze(numbers);
// Now, next line will thrown an error.
let luckyNum = numbers.pop();
console.log("luckyNum:", luckyNum, "numbers:", numbers);
I Hope this clarifies your doubts.
Even though it's a const, it will be modified and the new value will be [1,2,3,4].

Syntax - what does square brackets around a variable declaration mean [duplicate]

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

What is this syntax in JavaScript [duplicate]

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

Categories

Resources