use `this[]` to get private property inside the class in javascript [duplicate] - javascript

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

Related

what does # mean in javascript [duplicate]

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).

Create Javascript classes dynamically [duplicate]

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!

How to write Extensible methods in JavaScript [duplicate]

This question already has answers here:
Add method to string class
(6 answers)
How do I write an extension method in JavaScript?
(2 answers)
Closed 5 years ago.
I need to write a method I will call on a string literal in JavaScript. A method that I want to call:
"Javascript".toKampala();
Does that feature exist in JavaScript? and if it does How do I write such a method (toKampala()) on a JavaScript literal or any object?
In Kotlin I did it like this;
fun String.toHenry():String{
return "$this Henry";
}
and I can call
"chalres".toHenry()
Every string is default has a prototype, which is the String.prototype object and it can access anything which are defined there.
You need add that method in the String.prototype and it will be accessible from any string. You can access the current string in that function by this.
String.prototype.toHenry = function() {
return this + ' Hentry';
};
console.log('charles'.toHenry());

How to create the instance of the class by its string name? I tried to use `window` for these purposes also. [duplicate]

This question already has answers here:
How do I make JavaScript Object using a variable String to define the class name?
(10 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 5 years ago.
JS6, Browser
How to create the instance of the class by its string name? For example I have a string Foo and I need to create an instance of Foo class. I don't want to write such construction:
let className = getClassName(); // returns 'Foo', `Stuff` or other
...
let item = null;
if(`Foo` == className){
item = new Foo();
}
else if(`Stuff` == className){
item = new Stuff();
}
UPD
I try to get access to my class constructor through the window object but I have the problem: it returns undefined. But my class exists and browser knows it:
What you are trying to do is what you should do, in my opinion. This is a standard way of instantiating objects when the type of object is known at runtime, and is called the Factory Method Pattern.
You should avoid duplicating this everywhere in your code by using a factory object that will encapsulate the object creation. This factory object will have a method that will take the type name as its parameter.
Please use google: Instantiate a JavaScript Object Using a String to Define the Class Name
var myObject = window[classNameString];
P.S. I copy pasted your title of question and added javascript in front.

ES6 creating new instance with reflection [duplicate]

This question already has answers here:
Create an instance of a class in ES6 with a dynamic name? [duplicate]
(2 answers)
Closed 7 years ago.
I have a use case where I need to persist the name of a given class in a cookie so that I can create an instance of it later when the client returns. I would like to be able to do something like this:
class MyClass {
}
var a1 = new MyClass()
var className = a.class.name()//like in Java..
var a2 = Class.forName(className).newInstance();
Is this possible in ES6? Specifically I am looking for some way to get the name fo the class as a string,
You should be able to get the name of the class by calling a1.constructor.name1
Since classes are really fancy syntax for functions, if the class exists on the window object you could probably then do something like var a2 = new window[className](); but that really depends what scope you are working in.

Categories