2

I created model script in JS, so I can pass in an object and add in some logic and values. I use get/set to produce the values, however when i run my code it throws an RangeError: Maximum call stack size exceeded. Does anyone know who to fix this error?

var model = {
    get id() {
        return this.id ; 
    },
    set id(id) {
        this.id = id || '';
    },
    get filename(){
      return this.filename ; 
    },
    set filename(name){
      this.filename = name || ''; 
    },
    get savename(){
      return this.savename ; 
    },
    set savename(name){
      this.savename = name || ''; 
    },
    get filetype(){
      return this.filetype ; 
    },
    set filetype(name){
      this.filetype = name || ''; 
    }
};

module.exports = function(parm){
  model.id = parm.id ; 
  model.filename = parm.filename ; 
  model.savename = parm.savename ; 
  modle.filetype = parm.filetype;
  return model ; 
}; 

1 Answer 1

1
get id() {
    return this.id ; 
},

You've defined a getter for the property called "id". Your implementation of the getter says that it should return the value of the property "id" from the object.

In order to get the value of the property "id", the system first checks to see if there's a getter for that property. In this case, there is, so the getter is called.

Same goes for the setter.

You have to keep the actual property values somewhere else than as values of the object property or else you'll run into exactly the problem you're having.

get id() {
  return this._id;
},
set id(value) {
  this._id = value || "";
},

You don't have to do it exactly like that; it's just a way that works.

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

1 Comment

Thanks, the error was misleading. I guess because ill have both the get/set and the _var's, this might not be the most optimal way to achieve what i want.

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.