0

I'm missing something in understanding how js works. here's the problem:

We declare a module like this:

ns.obj = function() { // declare private variables var test = 1, test1 = 2; // declare some private function var myFunc=function(){test=2}; return{test:test, myFunc:myFunc}; } 

Every time myFunc is called, as we are not declaring test inside the function js should assume we are referring to the private variable.

The returned object makes sure we have test and myFunc visible if we have the module. So calling ns.obj.test should give us 1 at first. And after we call myFunc should give us 2. But it`s always 1. Why that happens?

here's the jsfiddle: http://jsfiddle.net/aXuwB/1/

2
  • 1
    I think your jsfiddle link is wrong Commented Aug 23, 2012 at 20:12
  • Yes, you are right, updated it. Commented Aug 24, 2012 at 4:42

3 Answers 3

1

In JavaScript overwriting a variable does not overwrite it anywhere else. You're effectively passing the number 1 in your return object; there is no reference to the test variable.

An option you have is returning a function. A function holds code and thus can hold a reference to a variable. Calling it would give you the variable: test: function() { return test; }.

Sign up to request clarification or add additional context in comments.

6 Comments

the test variable is specified in his function
@badunk: Yes, but he's retuning just the number value. Overwriting test does not overwrite it in the returned object.
Watch the jsfiddle attached. Absolutely same code for array gives different result. If I call push its ok and I can see the result. If I reinit the array it doesnt show up.
Calling myFunc does change the value of the private variable test to 2. But it does not change the value of testObj.test. That was initialized to 1 and is still 1 because nobody changed it. Arrays are reference objects; testArr is not an array. It is a reference to the array, and you put that same reference in testObj.testArr. Therefore, the same array is accessible by two different means, and changes to the array are therefore visible in two places. TL;DR: Read about the difference between reference types and value types.
thanks, Raymond, read it. But still not clear. When I make push its visible. When I make reassignment its not. Even if its a reference and I make it refer to a new instance, why isnt it visible?
|
1
var User = function () { var age = 0; // private this.incrementAge = function () { return age++; } return this.incrementAge(); // closure }; var firstUser = new User(); console.log('firstUser', firstUser.incrementAge()); // 1 var secondUser = new User(); console.log('secondUser', secondUser.incrementAge()); // 1 console.log('secondUser', secondUser.incrementAge()); // 2 

Comments

0

I think it has to do with the fact that you are declaring a new var test=1 everytime you execute the function. To clarify, everytime you call ns.obj(), you will be reassigning a new test variable to 1 and then returning it in your object. Thus, the value is always 1.

When you invoke the myFunc function, it will modify your private variable as you wanted, but you don't have a clean way to access the private variable (without resetting the value again).

Take a look at this and see if this demonstrates what you want:

var ns = {}; ns.obj = function(){ var test = 1; this.myFunc = function(){ test=2 }; this.getTest = function(){ return test; } } var testObj = new ns.obj(); console.log(testObj.getTest()); testObj.myFunc() console.log(testObj.getTest()); 

3 Comments

Its executed once to initialize object literal. After that Im just using it. So it`s not the case.
I am confused, when you say you are calling myFunc, it should return with 2 - but that function doesn't return any value
badunk, check the jsfiddle. It`s a module, function is called one time at start.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.