This question already has answers here:
Create object from class name in JavasScript ECMAScript 6
(8 answers)
ES6 classes : what about instrospection?
(1 answer)
Javascript ES6 class definition not accessible in window global
(2 answers)
Closed 2 years ago.
I have a class and a function, as follows:
class A {}
function B {}
I need to dynamically instantiate them, in a way similar to Java's class.forName().
I am able to instantiate the function as follows:
new window["B"]
However
window["A"] === undefined
Why the difference? Aren't classes just syntactic sugar for functions?
Related
This question already has answers here:
Benefits of using `Object.create` for inheritance
(4 answers)
What is the reason to use the 'new' keyword at Derived.prototype = new Base
(6 answers)
Correct javascript inheritance
(2 answers)
What is the correct prototype affectation in javascript inheritance?
(2 answers)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
EDIT:
I understand the purpose of Object.create() in general. But i need to know why it is used in the below code as it makes no difference at all.
I found the below code in this article. I can understand that fruits.call(this); will essentially inherit the properties from fruits to any instance of apple that gets created. Then why do we need to do apple.prototype = Object.create(fruits.prototype); Can anyone explain the purpose of this line? I tried executing the code without the that line and it still runs with no issues.
function fruits() {
this.name = 'fruit 1';
}
function apple() {
fruits.call(this);
}
apple.prototype = Object.create(fruits.prototype);
const app = new apple();
console.log(app.name);
// Output: fruit 1
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:
Should I be using object literals or constructor functions?
(12 answers)
Javascript Object : Literal Vs Constructor [duplicate]
(2 answers)
Object vs Class vs Function
(7 answers)
What is a difference between an object literal and a class with values in constructor in javascript?
(3 answers)
Closed 3 years ago.
I am currently finding it hard to figure out the difference between an object literal and a class.
Here's a link!
This might be helpful for you.
Content of the link:
The most significant difference I can see between creating classes and
literally notated objects is that the latter are entire objects,
whereas classes are not objects, but blueprints from which objects are
created.
This question already has answers here:
When do I use parentheses and when do I not?
(5 answers)
How to check if function exists in JavaScript?
(34 answers)
Closed 3 years ago.
I have an ES6 class with a static method. From another class, I wish to call that static method, but in some instances, the method may not exist.
how do I check if a static method exists before calling it?
class MyClassOne
{
static myStaticMethod()
{
.... do something
}
}
Thanks
class MyClassTwo
{
myFunc()
{
if(MyClassOne.myStaticMethod) <-- what should this be?
MyClassOne.myStaticMethod;
}
}
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Why is JavaScript bind() necessary?
(4 answers)
why do you need to bind a function in a constructor
(6 answers)
Closed 5 years ago.
In what circumstance is the .this keyword unbound? In my react code, I have found that I have to use this.someMethod = this.someMethod.bind(this) in the constructor() to access the properties of that method. Is this only when my method is triggered by an event listener, or is this necessary for all methods? Also are there any other situations that will break the access to .this?