This question already has answers here:
What does the `#` symbol do in JavaScript?
(3 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 11 days ago.
Came across something I cant find in docs. This #selectedIndex = 0; what does # do?
class WcTabPanel extends HTMLElement {
static observedAttributes = ["selected-index", "direction"];
#selectedIndex = 0;
appears in this example https://codepen.io/ndesmic/pen/mdELqbM
It means private field.
Class fields are public by default, but private class members can be created by using a hash # prefix. The privacy encapsulation of these class features is enforced by JavaScript itself.
You can find documentation here (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields).
Related
This question already has answers here:
What is the purpose of bivarianceHack in TypeScript types?
(2 answers)
Difference between Variance, Covariance, Contravariance and Bivariance in TypeScript
(1 answer)
Closed 6 months ago.
I've recently been doing more exploration in advance TS types and was going through react type declarations when I came Accross this
type RefCallback<T> = { bivarianceHack(instance: T | null): void }["bivarianceHack"];
can someone explain what ["bivarianceHack"] means and what it does? Not the string itself, but wondering why an array is declared after like type T = {}[] <-- []
This question already has answers here:
js dynamically access private fields (properties/members)
(5 answers)
Can I set a private class field using a variable as identifier? How?
(1 answer)
How to get access to private field via square brackets in JavaScript
(2 answers)
Closed last year.
It's acknowledge that we can access a property of an object by two ways :foo.bar and foo['bar'], but when I use the new feature # to declare a private member in class, it won't work properly:
class FooBar{
constructor(){}
testCall(name){
this[name]()
}
bar(){console.log(1)}
#bar(){console.log(2)}
}
let foo = new FooBar();
foo.testCall('bar'); //this line works properly
foo.testCall('#bar'); //Uncaught TypeError: this[name] is not a function
Can someone please tell how can I modify it to make the code work properly
This question already has answers here:
Arrow vs classic method in ES6 class
(1 answer)
What is the difference between class method vs. class field function vs. class field arrow function?
(2 answers)
Javascript class methods versus properties
(2 answers)
ES6 functions, arrow functions and 'this' in an ES6 class [duplicate]
(5 answers)
Closed 3 years ago.
I'm learning JavaScript/ReactJS and I'm finding I'm using functions with these variations:
public _someName = () => {} why the = before round brackets and why the lambda?
and
public _someName() {} What does this one NOT do compared to the above.
I've looked on SO and generally for some clear explanation of the difference but nothing has been found. Can someone tell me what the difference is based on my notes above.
Btw: I've read the docs!
This question already has answers here:
How do I make JavaScript Object using a variable String to define the class name?
(10 answers)
Create object from class name in JavasScript ECMAScript 6
(8 answers)
Closed 4 years ago.
I have a question which shouldn't be to difficult, but I've been spending days and can't seem to find the answer. All the solutions I found didn't help me, they all threw errors.
I made a Class in Javascript and want to create a new instance dynamically, so that I can instantiate multiple classes depending on the parameter in a function. It doesn't seem te work, and I wonder why.
settings.js and main.js are loaded via <script> tag in the HTML file, with settings.js being loaded first.
main.js
let myClass = new settings(); // Works
let myClass2 = test('settings'); // Error: Uncaught TypeError: param is not a constructor
function test(param){
return new param();
}
settings.js
class settings{
constructor(){
console.log("works");
}
}
Thanks for any help!
This question already has answers here:
Emulate super in javascript
(11 answers)
Closed 8 years ago.
I have to implement following code in Javascript, but cannot find equivalent of super.say() in javascript. How to do this translation ?
class Foo {
public void say() {
System.out.println("foo");
}
}
class Bar extends Foo {
static int counter = 0;
public void say() {
super.say();
counter++;
System.out.println("bar " + counter);
}
}
There's an article that describes it in detail: http://joshgertzen.com/object-oriented-super-class-method-calling-with-javascript/
Basically, you need to store a reference to your base method, then call it in the "derived" (perhaps "shadowed" might be a better word here) method.
The keyword super doesn't work in Javascript as in Java because there's no notion of classes. Without a class, you can't have a descendant, and thus super is useless in that context.
You could implement something super-like, as described here.