Calling static method from constructor es6 [duplicate] - javascript

This question already has answers here:
Call static methods from regular ES6 class methods
(3 answers)
Closed 6 years ago.
When I am trying to call a static method from constructor in javascript it says the method doesn't exist.
class TestClass {
constructor(){
this.staticMethod();
}
static staticMethod() {
}
}
This works fine if i try to call a normal method from constructor. If static methods belongs to class instead of instance why it's not allowing them to call from constructor?

this.constructor.staticMethod()
can be used to avoid referring to the class directly (particularly useful for class inheritance and pasted code).

You have to call it like this:
TestClass.staticMethod()

Related

How to get the method name of a getter on JavaScrpt? [duplicate]

This question already has answers here:
Is it possible to get a reference to the setter function of a "setter"?
(2 answers)
Closed 18 days ago.
Is there any way to get programatically the name of a getter method on Javascript?
For example:
class Example {
methodA() {}
get methodB() {}
}
console.log('works', Example.prototype.methodA.name);
console.log('fails', Example.prototype.methodB.name);
I want to do that because I am using a Proxy to handle an extension of a class and want to detect when the getter method is called.
Sure I can use:
if (prop === 'methodB')
But then, case the method is renamed I/anyone else will have to remember to rename the if checks. Example.prototype.methodA.name is always updated.
You can get the name of the method by using :
Object.getOwnPropertyDescriptor(obj, prop).get.name
Example:
class Example {
methodA() {}
get methodB() {}
}
const methodBGetter = Object.getOwnPropertyDescriptor(Example.prototype, 'methodB').get;
console.log(methodBGetter.name);
console.log will be "get methodB".

How to access the correct `this` inside a nested class method callback [duplicate]

This question already has answers here:
when calling class method by reference, how to have "this" still point to that class? [duplicate]
How to access the correct `this` inside a callback
(13 answers)
Closed 1 year ago.
Consider the below:
class foo(){
constructor(){this.x = 10}
f(){console.log(this.x)}
}
myfoo = new foo()
class bar{
constructor()
b(func){func()}
}
mybar = new bar()
mybar.b(myfoo.f())
Because of how this works in JavaScript, "this" in myfoo.f points to mybar instead of myfoo. How do I go around this? Also, why does JavaScript do this?
Response to duplicate
Before reporting this as a duplicate again, please note that this question is directed to the JavaScript class, not functions in this link. I don't think that link does not resolve my issue.

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

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.

Possible to override a base classes privileged function? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there an equivalent of PHP's 'parent' with javascript prototypal inheritance?
Override a base class function
I have a base class/prototype & a child class/prototype in Javascript.
Is it possible to make the child class override a base classes privileged function?
I know I am trying to treat javascript as an OO language which its not but you never know this maybe possible?
If its possible could you give an example how I do this?
function baseClass()
{
this.privFunct() {}
}
function childClass()
{
this.privFunct()
{
var baseFunct = baseClass.prototype.privFunct;
this.baseFunct();
// no do some object specific actions here
}
}

Categories

Resources