1

I want to call a non-static method from static method. Both are in same class. How can I achieve this ?

class Home extends React.Component {

constructor(props) {
    super(props);
    console.log("in constructor props =", this.props.mainData);
    this.state = {
        data: null,
        isFetch: false,
        clickEvent: false
    }
    this.allDataShow = this.allDataShow.bind(this);
    this.upcomingShow =  this.upcomingShow.bind(this);
}

allDataShow(){
    allData(this.props.mainData);
}

upcomingShow(){
    upcoming(this.props.mainData);
}


static changeData(option) {
    console.log("I'm home changeData");
    switch (option) {
        case "All":
            console.log("All");
            allDataShow();
            break;

        case "Upcoming":
            console.log("Upcoming");
            console.log("this",this);
            inst.prototype.upcomingShow();
            break;
   }
}
render(){...}
}

This is updated code in which I am calling changeData in another component, and in changeData I call non-static method. But it doesn't work.

2 Answers 2

4

The non-static method will be on the prototype, so reference My.prototype or this.prototype (this will refer to My inside the static method):

class My{
    static my1(){
        this.prototype.my2();
    }
    my2(){
        console.log("my2 is executing");
    }
}

My.my1();

That said, this is very weird - non-static methods are generally useful to refer to and use instance data. If the method uses instance data, it will need an instance to run sensibly. If the method doesn't use instance data, it probably shouldn't be a prototype method, but a static method or a standalone function.

Sign up to request clarification or add additional context in comments.

4 Comments

I used your solution. But it gives error "TypeError: Cannot read property 'my2' of undefined". Actually my question is related to React.js.
Press "Run code snippet" to see the code working - it logs my2 is executing as desired. If it doesn't work in the code on your machine, please post the code on your machine, since it's different from what you have in the question.
my code is related to React.js. I updated my code please take a look.
Your question still shows the same code, with no React involved. The code in my answer solves your problem for the code you've posted
2

You must instantiate a new "My" class and then call from there. You can either require the class as a parameter or make a new one in the method.

class My{
    static my1(instance){
        //something like this
        instance.my2();
    }
    my2(){
        console.log("my2 is executing");
    }
}

var myInstance = new My();
My.my1(myInstance);

OR

class MyOther{
    static my1(){
        //something like this
        var myInstance = new My();
        myInstance.my2();
    }
    my2(){
        console.log("my2 is executing");
    }
}

MyOther.my1();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.