admin 管理员组文章数量: 1086019
I am unable to call private or non static method from static method in class, below is the example
class a {
fun1(){
console.log('fun1');
}
static staticfun(){
console.log('staticfun');
this.fun1();
}
}
a.staticfun();
I am trying to expose only staticfun method which internally calls all private methods, but this gives me this.fun1
is not a function. I tried to find many ways to find it with 'this', but it does work.
How do I call private instance methods inside static methods?
I am unable to call private or non static method from static method in class, below is the example
class a {
fun1(){
console.log('fun1');
}
static staticfun(){
console.log('staticfun');
this.fun1();
}
}
a.staticfun();
I am trying to expose only staticfun method which internally calls all private methods, but this gives me this.fun1
is not a function. I tried to find many ways to find it with 'this', but it does work.
How do I call private instance methods inside static methods?
Share Improve this question edited Oct 23, 2018 at 10:52 Blue 22.9k7 gold badges66 silver badges98 bronze badges asked Oct 23, 2018 at 10:36 Tapan DaveTapan Dave 2731 gold badge3 silver badges17 bronze badges 4- There are no "private" methods in JavaScript. Do you mean instance methods? – Bergi Commented Oct 23, 2018 at 10:48
-
It's unclear in your example, but is there a reason why you can't just make
fun1
static? – Bergi Commented Oct 23, 2018 at 10:48 - i am creating node js micro service where i am returning promise from module.exports {return new promise { promise.all().then(r=>fun2).then(r=> fun2)....at last i resolve()}} there i want to make all the promise and function to be wrapped inside class and function should not expose outside the class like modules.exports = {class a{static promise(){.....} fun1(){} fun2(){}} return a.promise()} – Tapan Dave Commented Oct 23, 2018 at 12:53
-
1
a) avoid the
Promise
constructor antipattern b) there is no reason to wrap anything inclass
syntax if you don't need multiple instances – Bergi Commented Oct 23, 2018 at 13:13
3 Answers
Reset to default 5Another way is to call the function directly from the class prototype (meaning literally the prototype
property, not __proto__
), if you want to avoid instantiating it.
class a {
fun1(){
console.log('fun1');
}
static staticfun(){
console.log('staticfun');
this.prototype.fun1();
}
}
a.staticfun();
fun1
is not a static function, so you need to define a new instance of the a
class in order to call it:
class a {
fun1() {
console.log('fun1');
}
static staticfun() {
console.log('staticfun');
new this().fun1();
}
}
a.staticfun();
You should note that this is not good practice, though. You shouldn't have a static method relying on non-static logic.
A workaround would be to pass an instance of a
to the static function, but that pletely defies the point of having a static method in the first place.
First, read this SO question
It's possible, You can create an instance of class a
and then invoke from the instance the method fun1
.
Although, There is no sense to call a non-static method from a static one.
static means that this method belong to the object (not to the instance)
class a {
fun1(){
console.log('fun1');
}
static staticfun(){
console.log('staticfun');
const classInstance = new a()
classInstance.fun1();
}
}
a.staticfun();
本文标签: javascriptInvoking private method from static method ES6Stack Overflow
版权声明:本文标题:javascript - Invoking private method from static method ES6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744037056a2522562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论