Javascript - How do you call a function inside a class from within that class?

后端 未结 5 2118
难免孤独
难免孤独 2021-02-18 13:25

I am trying to call the function MyMethod from within a object but none of the syntax below works. There must be a really obvious error below but I can\'t see it.



        
5条回答
  •  孤街浪徒
    2021-02-18 13:30

    var MyObject = function MyObject() {
           this.MyMethod = function () {
             alert('It works');
           } }
    
    var test = new MyObject(); test.MyMethod();
    

    The above will do. Or you can just create a constructor and call this method inside that. So at the time of object creation it will call this.MyMethod()

提交回复
热议问题