0

I am getting following error for the setter defined in the javascript: "RangeError: Maximum call stack size exceeded".

The code4 is as follows:

setter defn :

this.__defineSetter('_myList', function(list)  
        {
              log.debug("in setter ....");  
             if(this._myList == list)  
            {  
                 log.debug("in setter..");    
                 return;           

            }  
            this._myList = list;  
         });  

call:

myMethod = function(msg)    
{  
  try  
    {       
     this.myList = msg.myList;  
    }catch(e)
    {
        log.debug("error in calling setter... " + e);
    }  
}

I am unable to figure out why is it going to infinite loop??

2
  • Doesn't call the last statement in the setter function the same function again? Commented Jul 6, 2012 at 7:04
  • 1
    yeah you have to store the value else where, this is a bad pattern and use of __defineSetter() has been deprecated: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… i would avoid these APIs if i were you Commented Jul 6, 2012 at 7:07

2 Answers 2

2

When you call

this._myList = list; 

it invokes the defined setter, which causes the infinite recursion.

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

Comments

1

The problem is that you call the setter from within the setter...

this._myList = list;

Should create another 'private' variable to store the value. Something like this...

var _myInnerList;

this.__defineSetter__('_myList', function(list) {
  log.debug("in setter ....");  
  if(_myInnerList === list) {  
    log.debug("in setter..");    
    return;           
  }  
  _myInnerList = list;  
});  

Also use === for comparisons (always) and change __defineSetter into...

__defineSetter__

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.