How to `new class` from a class name in a variable? [duplicate] - javascript

This question already has answers here:
"Variable" variables in JavaScript
(8 answers)
Create object from class name in JavasScript ECMAScript 6
(8 answers)
Closed last month.
class Shape {}
let myClass = 'Shape';
new myClass();
This looks for a class myClass and fails, I have no idea how to get it to try and create a Shape class.
In PHP it would be simple:
class Shape {}
$myClass = 'Shape';
new $myClass();
How can I do this in JavaScript?
I've tried new window[myClass](), it doesn't work with ES6 classes.

Related

Class method for square brackets accessor JavaScript [duplicate]

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]
}
}

ES6 class instance from string [duplicate]

This question already has answers here:
Create object from class name in JavasScript ECMAScript 6
(8 answers)
Closed 4 years ago.
I try to setup an instance dynamically from a string. I've read many questions about it but the answers does not work for me.
It says it's possible to use window before the name to set the instance. It does not work.
class MyClass {
// Something useful
}
let params = {};
let name = 'MyClass';
let instance = new window[name](params);
I've also tried to do this without luck (throws error):
let instance = new window['MyClass'](params);
However, this works:
let instance = new MyClass(params);
Why can't I use window in this case? Any other ideas?
Only global variables are put into window automatically.
Create an object that maps from class names to classes:
const classMap = {
"MyClass": MyClass,
"MyClass2": MyClass2,
...
};
Then use classMap[name](params) rather than window[name](params).

How to have a class which is a property of something? [duplicate]

This question already has answers here:
How to namespace es6 classes (for React components)
(3 answers)
Closed 6 years ago.
I have a javascript object menu, and I want to add a property controller which should be a constructor. However, the following gives a syntax error:
class menu.foobar {
// stuff here
}
What is the right way to do this?
Use a class expression:
menu.foobar = class {}

How to instantiate class dynamically [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 8 years ago.
I have an array with names of constructors var arr = ['Class1', 'Class2', 'Class3'].
function Class1() {
this.name = 'class1';
}
Is it possible to dynamically create instances of these classes? I mean something like
var class1Object = new arr[0]();
I tried that but it isn't working (Uncaught TypeError: string is not a function).
functions defined in "global" scope are actually created on the window object, so you can do this (as long as the code is in the head of the page, and not scoped to something specific):
function Class1(){
this.name = 'class1';
}
var className = "Class1";
var c1 = new window[className]();
Live example: http://jsfiddle.net/vdf4W/
Previously answered in this post (Google: Dynamic Instantiation In JavaScript)
Dynamic Instantiation In JavaScript

Creating "static" members in a javascript object [duplicate]

This question already has answers here:
Static variables in JavaScript
(43 answers)
Closed 8 years ago.
Is there a way to create "static" members in a JS Object?
function Person(){
}
Person.prototype.age = null;
Person.prototype.gender = null;
I would like to add personsCount as a static member, is that possible?
Sure, just add Person.personsCount without the prototype
Common practise is to make such "static members" properties of the constructor function itself:
function Person() {
Person.count++;
}
Person.count = 0;

Categories

Resources