Class method for square brackets accessor JavaScript [duplicate] - javascript

This question already has answers here:
Is it possible to implement dynamic getters/setters in JavaScript?
(5 answers)
JavaScript getter for all properties
(9 answers)
Closed 12 days ago.
Is there a way to override square-bracket accessor on a class, i.e.:
class Foo {
constructor() {
this.bar = {}
}
[prop]() {
return this.bar[prop]
}
}

Related

How are these methods of writing functions different? [duplicate]

This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
ES6 Object Literal Syntax for Methods
(1 answer)
Closed 2 years ago.
I have code that has the following functions written:
testing: function() {
///
}
what is the difference of writing it as :
testing() {
///
}
Do they mean the same thing and have the same purpose?

What is this syntax for creating an object? [duplicate]

This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
No colon after property name in object declaration, is it valid? [duplicate]
(2 answers)
Closed 2 years ago.
I'm reading about mixins here and it uses this valid code:
let sayHiMixin = {
sayHi() {
alert(`Hello ${this.name}`);
},
sayBye() {
alert(`Bye ${this.name}`);
}
};
console.log(sayHiMixin);
sayHiMixin is a valid object. Can anyone point out to me how the syntax for writing this object is valid (maybe an MDN page)? I've never seen it before except for when writing methods in a class.

Parameters in class name [duplicate]

This question already has answers here:
How to dynamically add a class to manual class names?
(16 answers)
Closed 4 years ago.
How can I pass parameters to a class name from a function?
funcName = (param) => {
return (<div className='param'>
..............
</div>) }
Use {} to inject JS code in the JSX layout:
<div className={my_parameter}>

How to make properties private in classes of Javascript 6? Purpose of setters and getters? [duplicate]

This question already has answers here:
Private properties in JavaScript ES6 classes
(41 answers)
Is the underscore prefix for property and method names merely a convention?
(6 answers)
Closed 5 years ago.
Look at the following code:
class Animal() {
constructor(type, name) {
this._type = type;
this._name = name;
}
get type() {
return this._type;
}
get name() {
return this._name;
}
}
If I were to run:
var Dog = new Animal("Dog", "Rover");
console.log(Dog._name);
The console would return the name of my dog without the need to use the `get name() {} that I wrote above. What is the point of getters and setters in Javascript 6 if I can access my object properties directly? Why is the underscore a convention for private properties when it does nothing to privatize them? How can I make my properties private in classes in Javascript using current ecmascript-6 standards?

ES6 class member to be a class itself [duplicate]

This question already has answers here:
Nested ES6 classes?
(4 answers)
Closed 6 years ago.
in es6 a class can have methods and static methods as its members. can it also have classes as members? (because Class is actually kind of function itself?)
i would like it to build nested classes.
class foo {
barMethod {
// do something for the instance
}
static bamMethod {
// do something
}
// e.g.
class batClass {
// the constructor and other stuff for batClass
}
}
new foo.batClass();
[No.] (Nested ES6 classes?). Read this post. You can only do as #floribon mentioned in comment.

Categories

Resources