Calling method inside another method in javascript?

前端 未结 4 1548
执念已碎
执念已碎 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:59

    I think the problem is that when this.C() is executed inside the function referred to by B, this refers to the object that contains B, that is, object A. (This assumes B() is called within the context of A)

    The problem is, C does not exist on the object A, since it's defined within B. If you want to call a local function C() within B, just use C().

    EDIT: Also, I'm not sure what you've posted is valid JavaScript. Specifically, B should be defined this way, since you can't use the object:property syntax within a function.

    B: function()
    {
      var test='test';
      var result='t1';
    
      var C = function()
      {
        this.test='test1';
        return 'test1';    
      }
    
     result=C();  
     return result; 
    }
    

提交回复
热议问题