Javascript square brackets around method name [duplicate] - javascript

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

Related

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

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

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?

What does exclamation point after variable mean in JavaScript? [duplicate]

This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Closed 5 years ago.
I've seen in some React/TypeScript implementations such as :
ref={ ref => this.container = ref! }
What does the exclamation point means in ref!?
Is that something specific in TypeScript, or a new standard JavaScript notation?
In TypeScript, a postfix ! removes null and undefined from the type of an expression.
This is useful when you know, for reasons outside TypeScript's inference ability, that a variable that "could" be null or undefined actually isn't.

Significance of defining custom methods to window object in javascript [duplicate]

This question already has answers here:
What's the difference between a global variable and a 'window.variable' in JavaScript?
(5 answers)
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 5 years ago.
I am working on a project in javascript and while going through design pattern concepts came across following point.
I have a sample code as below:
window.customfun = (function(){}())
Here i have two questions:
1) Can we not simply define a global function as below:
customfun = (function(){}())
2) (function(){}()) is an anonymous function with its own evaluation environment but i have learnt that to be implemented as : (funtion(){})()see the position of parentheses around couple of parentheses. I found that both are working so i wanted to know if there is any difference between them?
Sure, you can define global functions
No, there is actually no difference.
Is there a difference: (1+(1)) === (1) + (1) ?

jScript/HTML5 - What's the difference between localStorage.setItem & localStorage.variable = x? [duplicate]

This question already has answers here:
localStorage - use getItem/setItem functions or access object directly?
(5 answers)
Closed 8 years ago.
What is the difference between the following two snippets of code? Is the square bracket syntax an old, deprecated syntax? When I first used localStorage, all the documentation I found definitely said to use the square bracket syntax, but now I can't find any documentation on it at all.
The documented syntax:
localStorage.setItem('hello', 'world');
localStorage.getItem('hello'); // world
The square bracket syntax:
localStorage.hello = 'world';
localStorage.hello; // world
This is what the documentation says about it:
Although the values can be set and read using the standard JavaScript property access method, using the getItem and setItem methods is recommended.

Categories