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;
}
}
Related
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?
This question already has answers here:
setTimeout and "this" in JavaScript
(5 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 2 years ago.
I'm having this error when I call a method from another method in a class. This method is been called from setInterval.
class App {
b() {
console.log("BBBBBBBB")
}
t() {
console.log("TTTTTTT")
this.b();
}
}
const t = new App();
setInterval(t.t, 1000);
You need to bind the method to the variable so the value of this stays constant. Read this page for more information.
setInterval(t.t.bind(t), 1000);
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:
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());
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.