Calling method inside another method in javascript?

前端 未结 4 1525
北荒
北荒 2021-02-07 21:05

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 22:01

    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.

提交回复
热议问题