0

I am new to javascript and I have written a code like this

file myclass.js
//----------------------------------------------

function myclass () {
     this.functionA = function (value) { 
       var _this = this;
       _this.functionB ();

      }

    this.functionB = function () { 
     // here I am using the value passed to functionA when it is called.
        alert(value);
      }
}

//------------------------------------------------------------------

file main.js
//-----------------------------------------   
    mc = new myclass();
    mc.functionA (45);
//-------------------------------------

Here I am totally confused that I am my main file I have called a functionA passed an argument and When I have called functionB in functionA I haven't passed the argument in functionB but still I am able to access it. Can any one kindly explain that how is it possible ??

P.S value is not global and not used any where else

Thanks

A_user
  • 2,087
  • 6
  • 25
  • 33
  • It's called a **closure**. Read more about it here: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work – Andrei Андрей Листочкин Jun 18 '12 at 06:43
  • 1
    thank for answer but its not closure.because I am not defing functionB inside functionB . I am only calling it – A_user Jun 18 '12 at 06:45
  • 2
    I suggest you create a fully-working example showing the unexpected behavior. This snippet is not enough to work from. – Kenan Banks Jun 18 '12 at 06:47
  • thanks @nhahtdh is there a way to find out what this is referring to any current time – A_user Jun 18 '12 at 06:54
  • make sure , `value` isn't declared as global - even with `var` in global scope,( is there where `this` points to window object) – abuduba Jun 18 '12 at 06:55
  • @A_user: I think you should consult a good book on Javascript, and also write sample code and play around with it. Putting alert to check is also one way, but it don't aid in your understanding. – nhahtdh Jun 18 '12 at 06:57
  • @A_user are you referencing any other js files? whats the value being alerted as, is it actually 45? – Samy Vilar Jun 18 '12 at 07:20

1 Answers1

1

I can't reproduce your behavior but I assume that you have another variable defined in the outer scope called value which is passed to functionA as a parameter. So what you see is not one but two variables with the same names and values.

Something like that:

function SomeConstructor() {
    var value = 'Surprise'; // * VALUE_1

    this.functionA = function (value) { // VALUE_2
        var _this = this;
        _this.functionB ();
    }

    this.functionB = function () { 
        // Here you're using the value from starred line above
        // VALUE_1
        alert(value);
    }

    // Here you also pass the same value and later assumes that 
    // functionB sees it as a parameter (VALUE_2)
    functionA(value);
}

Notice that if you rename value parameter for functionA and the value variable in the outer scope all confusion goes away:

function SomeConstructor() {
    var value1 = 'Surprise';

    this.functionA = function (value2) {
        var _this = this;
        _this.functionB ();
    }

    this.functionB = function () { 
        // Here you're using the value from starred line above
        alert(value1);
    }

    // Here it's clear that the parameter you're passing is not used at all
    functionA(value1);
}

Check your code and see if that's the case.


Edit: After you've edited your question I still can't reproduce your case. Check if that's the only script on a page. Perhaps, you have some browser addons or extensions installed that add the code to your page. In Chrome Dev Tools I get:

"ReferenceError: value is not defined"