What's the difference between using Array.of() compared with brackets [ ]? [duplicate] - javascript

This question already has answers here:
Array.of vs "[ ]". When to use Array.of over "[ ]"?
(3 answers)
What’s the difference between "Array()" and "[]" while declaring a JavaScript array?
(19 answers)
Closed 4 years ago.
For example,
let x = [1,2,3,5];
is equivalent to:
let x = Array.of(1,2,3,4,5);
(Unless I'm missing an important detail, which is why I'm asking the question)
You could also mix these with spread ... syntax and variables and thus other arrays. To me, it seems Array.of() has more overhead. Would Array.of() have to parse an arguments object into another array?
I know there's also new Array() as others have before questioned here, but that has a different semantic purpose, so I don't see this question as a duplicate to that.
As I see it now, Array.of() and [ ] seem redundant. The function's intent does seem more explicit on the former, but the latter's intent is simple enough to not be misunderstood.
So to summarize:
When is one preferable over the other?
Why does Array.of() exist when JavaScript survived without it for so long?
And, what're the differences of these two methods, if any? Would there be any needless overhead?

Related

How to use filter to compare to arrays to find missing object? [duplicate]

This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
Closed 9 months ago.
I'm trying to use arr.filter() to find the missing element between two arrays, this has been answered plenty of times and i've seen threads like this one Finding missing element in two array for javascript that explain it actually very well. For some reason thought, i cant seem to get this to work.
This is my code
var x = [{Number:1},{Number:2},{Number:3}]
var y = [{Number:1},{Number:2}]
function getDifference(x,y){
x.filter(function(object,index,arr){
console.log(object,index,arr)
if(!y.includes(object)){
// print object
console.log(object) // <-- Prints all 3, should just print the missing one.
}
})
}
getDifference(x,y)
Basically, it just needs to print out the missing object. Which in this case, is {Number:3}
Instead, it prints every object.
I've tried with the code in the thread that i linked above, and still was having trouble. Not sure what i'm not understanding with it.

array-like in Javascript detail explanations [duplicate]

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Does javascript have a method to replace part of a string without creating a new string?
(4 answers)
Closed 1 year ago.
var a = 'cat' ;
a[0] = 'r' ;
a = 'cat'
Why..??
In case of string although you can access elements by array notation, if you try to change its content it will fail silently i.e. will not throw any error but will not change content either.
Please explain me detail.
Strings are primitive values in javascript, and are therefore immutable. This is just how the language works so there's not much to explain besides that. You can read more about it here!
It is not throwing an error because you're probably not running it in strict mode.
Strings are immutable, in JavaScript only objects and arrays are mutable. You can search for mutable data types in JavaScript in Google.
"A mutable object is an object whose state can be modified after it is created." MDN.
You can read more here: Mutable

Javascript square brackets around method name [duplicate]

This question already has answers here:
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 5 years ago.
In the RxJs doc I found following code snippet:
[rxSubscriberSymbol]() {
return new SubjectSubscriber(this);
}
Its part of the Subject source code and is the first method right after the constructor.
So what do square brackets mean in this context?
those are symbols, which is very similar to defining properties but gives different accessibility and testability functionality and they are completely unique,
you can read a lot more about metaprogramming here,
Metaprogramming in ES6: Symbols and why they're awesome

Is Object Destructuring in function parameters a good pattern? [duplicate]

This question already has answers here:
Multiple arguments vs. options object
(10 answers)
Closed 6 years ago.
I've really fallen in love with object destructuring with functions.
For example:
var buyCoffee = function({sku, pounds = 1, roast:''}){
...more code
}
buyCoffee({sku:"cf-100" pounds: 3, roast: 'dark'});
buyCoffee({sku:"cf-101" roast: 'light'});
Pros
Flexibility similar to the args object.
Added Simplicity
Not required to put in parameters if I don't need them.
Cons
Variable names are locked all the way through.
Currying would be much harder.(From what I can tell)
Significant computational overhead vs traditional params ?
Harder to test?
I'd like to know what downsides there are to this approach? Is this a good pattern to use as I grow as a developer? Just looking for some wisdom from the trenches on this. Thoughts?
Variable names are locked all the way through.
Not at all. You can easily destructure into arbitrary variables:
function({sku:mySku, pounds:localPounds=1, roast=''}) { … // use mySku, localPounds and roast
Currying would be much harder.
There's no currying when you pass objects anyway. And currying with optional parameters is always hard.
Potential computational overhead?
Depends on what you compare it against.

What is the most efficient way to empty a plain object in javascript? [duplicate]

This question already has answers here:
How to quickly clear a JavaScript Object?
(11 answers)
Closed 9 years ago.
As stated Here, It seems that the most efficient way to empty an existing array (and NOT to allocate a new one) in javascript is to use:
array.length = 0;
Does the same operation work for plain objects? (aka "associative arrays" or "dictionaries")
If not, what is the most efficient way to empty an existing javascript object?
I think that allocating a new one is not the best option, since it will push some extra work to the garbage collector, and will allocate some new memory on the heap, but I might be wrong.
I need a solution that works at least with Chrome and Firefox.
The shortest way to do that is to create a new object. You'd end up garbage collecting all the properties anyway.
var foo = {
a: 5,
b: 4
};
foo = {};
You could also iterate over the properties and remove them individually:
for (var prop in foo) {
if (foo.hasOwnProperty(prop)) {
delete foo[prop];
}
}
It's also worth pointing out, as a matter of verbiage, that JavaScript has objects with properties, not associative arrays.

Categories