0

I have the following function call:

var qid = 6;
var $scope.qidLower = null;
var $scope.qidUpper = null;
_o.parseRange(qid, $scope.qidLower, $scope.qidUpper);
// Checking after here the values of $scope.qidLower and $scope.qidUpper are null !

This is passed to my function:

  var _parseRange = function (text, lower, upper) {
       if (!text || text === "") {
           lower = null;
           upper = null;
       }
       else if (text.indexOf("-") > 0) {
           arr = text.split("-");
           lower = +arr[0];
           upper = +arr[1];
       }
       else {
           lower = +text;
           upper = null;
       }
   }

When I check the values of lower and upper the function sets them to 6 and null

When I now $scope.qidLower, $scope.qidUpper on the line after the function call they are both null.

Can someone explain why this is happening. I thought if the function modifies a parameter value that it would be available after the function returns.

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • Practically what you want to know how JS handles arguments that are passed to functions, which is normally done using pass by reference or pass by value. In JavaScript it is kind of special, see the question given above. – dirkk Feb 26 '14 at 12:45

2 Answers2

0

When you pass $scope.qidLower and $scope.qidUpper as parameters to a function. The references are copied to the parameters.

When you assign values (new references) to these parameters inside the function, the references of the parameters change, but the $scope.qidLower and $scope.qidUpper don't

Solution:

var _parseRange = function (text, lower, upper) {
       if (!text || text === "") {
           lower = null;
           upper = null;
       }
       else if (text.indexOf("-") > 0) {
           arr = text.split("-");
           lower = +arr[0];
           upper = +arr[1];
       }
       else {
           lower = +text;
           upper = null;
       }
       return {lower:lower,upper:upper}; //return at the end of the function
   }

Change your code:

var qid = 6;
var $scope.qidLower = null;
var $scope.qidUpper = null;
var calculatedBounds = _o.parseRange(qid, $scope.qidLower, $scope.qidUpper);
$scope.qidLower = calculatedBounds.lower;
$scope.qidUpper = calculatedBounds.upper;

Note:

If you assign a non-null objects to your $scope.qidLower and $scope.qidUpper and inside the function, you update the parameters' properties instead of assigning new objects, your $scope.qidLower and $scope.qidUpper will notice the changes.

var _parseRange = function (text, bounds) {
           if (!text || text === "") {
               bounds.lower = null;
               bounds.upper = null;
           }
           else if (text.indexOf("-") > 0) {
               arr = text.split("-");
               bounds.lower = +arr[0];
               bounds.upper = +arr[1];
           }
           else {
               bounds.lower = +text;
               bounds.upper = null;
           }
       }

Call the function:

 var qid = 6;
 var $scope.bounds = {
     qidLower : null,
     qidUpper : null
 };
 _o.parseRange(qid,$scope.bounds);
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
0

Try this way

var qid = 6;
$scope.qidLower = $scope.qidLower;
$scope.qidUpper = $scope.qidLower;
_o.parseRange(qid, $scope.qidLower, $scope.qidUpper)

and

var _parseRange = function (text, lower, upper) {
       if (!text || text === "" || text === undefined) {
           lower = null;
           upper = null;
       }
       else if (text.indexOf("-") > 0) {
           arr = text.split("-");
           lower = +arr[0];
           upper = +arr[1];
       }
       else {
           lower = +text;
           upper = null;
       }
        $scope.qidLower = lower ;
        $scope.qidUpper = upper ;
   }

Now you can get the values Any when and any where from

$scope.qidLower and $scope.qidUpper

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234