Calling method inside another method in javascript?

前端 未结 4 1544
执念已碎
执念已碎 2021-02-07 21:21

I am having a JavaScript namespace say

A={

  CA: function() {
    this.B();
  },
  B: function() {
    var test=\'test\';
    var result=\'t1\';

    C: functi         


        
4条回答
  •  遥遥无期
    2021-02-07 21:45

    Solution for calling methods from another method. (Essentially the pointer "this" must be assigned to a variable and the new variable used in place of this.)

     function myfunction(){
        var me = this;
    
        me.method1 = function(msg){ alert(msg); }
    
        me.method2 = function(){ 
           me.method1("method1 called from method2"); 
        }
     }
    
     var f as new myfunction();
     f.method2();
    

    This example shows how one can call a method from within another method or from outside using an instance of the function.

提交回复
热议问题