I have a class with a static method:
class User {
constructor() {
User.staticMethod();
}
static staticMethod() {}
}
Is there som
static members of a class in javascript are added as class properties, you can see the list by using code
console.log(Object.getOwnPropertyNames(PaymentStrategy));
and for accessing the member just
const memberFun = PaymentStrategy["memberName"];
//if member is a function then execute
memberFun();
//or
memberFun(data);
example:
class PaymentStrategy{
static Card(user){
console.log(`${user} will pay with a credit card`);
}
static PayPal(user){
console.log(`${user} will pay through paypal`);
}
static BKash(user){
console.log(`${user} will pay through Bkash`);
}
}
export default PaymentStrategy;
import PaymentStrategy from "./PaymentStrategy";
class Payment{
constructor(strategy = "BKash"){
this.strategy = PaymentStrategy[strategy];
console.log(Object.getOwnPropertyNames(PaymentStrategy));
}
changeStrategy(newStratgey){
this.strategy = PaymentStrategy[newStratgey];
console.log(`***Payment Strategy Changed***`);
}
showPaymentMethod(user){
this.strategy(user);
}
}
export default Payment;
```
```
import Payment from "./Payment"
const strategyPattern = (()=>{
const execute = ()=>{
const paymentMethod = new Payment();
paymentMethod.showPaymentMethod("Suru");
paymentMethod.showPaymentMethod("Nora");
paymentMethod.changeStrategy("PayPal");
paymentMethod.showPaymentMethod("Rafiq");
}
return{
execute:execute
}
})();
export {strategyPattern}
```
In @Ninjaneer 's accepted answer:
class StaticMethodCall { constructor() {
console.log(StaticMethodCall.staticMethod());
// 'static method has been called.'
console.log(this.constructor.staticMethod());
// 'static method has been called.' }
static staticMethod() {
return 'static method has been called.'; } }
this.constructor.staticMethod()
will throw an error because you must call the super constructor before accessing this
in the constructor.
Assuming you fix this aforementioned issue, no pun intended, I think the only benefit of calling this.constructor.staticMethod()
is that you're not dependent on the name of the class -- if it ever changes. However, this benefit is probably insignificant since it only benefits static method calls made from inside the class. Static method calls made externally would have to be something like StaticMethodCall.constructor.staticMethod()
which is defeats the purpose and looks silly.
In Summary:
If you plan on using the static method outside of the class, I'd definitely stick with using StaticMethodCall.staticMethod() since it's more idiomatic and concise.
If you only plan on using the static method within the class, then maybe it's ok to to use this.constructor.staticMethod()
, but it'll probably confuse other developers and lead them back to this page :-)
static
things bind to class rather than instance. So you must at least specify the class name.
If you don't want to bind to them to a class make them global.
From MDN documentation
Static method calls are made directly on the class and are not callable on instances of the class. Static methods are often used to create utility functions.
For more please see=> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static
You can do something like this => this.constructor.staticMethod());
to call static method.
class StaticMethodCall {
constructor() {
console.log(StaticMethodCall.staticMethod());
// 'static method has been called.'
console.log(this.constructor.staticMethod());
// 'static method has been called.'
}
static staticMethod() {
return 'static method has been called.';
}
}
instead of this User.staticMethod() you can add this.constructor.staticMethod()