Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getter setter Maximum call stack size exceeded Error

Tags:

javascript

I am trying to learn get and set in JavaScript object for that I tried

function ab(n){this.name=n;}; 
var c= new ab("abcde");  
console.log(c);
Object.defineProperty(c, 'name', {
    get: function() {
        return name;
    },
    set: function(Name) {
       this.name = Name;
        }
}); 
c.name="xyz";  
console.log(c.name); 

Here I am first making object using constructor and then using get and set. But i am getting error "Maximum call stack size exceeded". I am not getting the reason of This Error . Thanks for help

like image 762
Roli Agrawal Avatar asked May 28 '16 17:05

Roli Agrawal


People also ask

How do I fix error max call stack size exceeded?

The call stack is limited in size, and when it's exceeded, the RangeError is thrown. This can happen when a deeply nested function is called, or when a lot of new variables are created. The most common way to fix this error is to reduce the number of function calls, or to limit the number of variables that are created.

How do I fix uncaught RangeError max call stack size exceeded see JavaScript console for details?

The "RangeError: Maximum call stack size exceeded" error occurs when a function is called so many times that the invocations exceed the call stack limit. To solve the error, specify a base case that has to be met to exit the recursion.

What does rangeerror maximum call stack size exceeded mean?

The "RangeError: Maximum call stack size exceeded" error occurs when a function is being called so many times that the invocations exceed the call stack limit. To solve the error, specify a base case that has to be met to exit the recursion.

Why does NPM say maximum call stack size exceeded?

Maximum call stack size exceeded The detail of the error may vary inside your npm-debug.log file, but this error is commonly caused by NPM unable to complete a process related to your npm install command.

What is a call stack?

The call stack is a data structure that keeps track of which functions have been called, and in what order. It's used by the JavaScript engine to determine which function to call next, and to keep track of the program's current state.


2 Answers

I think it has already been explained, that this.name = Name in the setter calls again the setter, wich leads to an infinite recursion.

How about this aproach:

function Ab(_name){
    Object.defineProperty(this, "name", {
        //enumerable: true, //maybe?
        get: function(){ return _name },
        set: function(value){ _name = value }
    });
}

var c = new Ab("abcde");
console.log(c, c.name);

Or the prototypal-approach
downside: the private property _name is a public

function Ab(n){
    this.name = n;
}
Object.defineProperty(Ab.prototype, "name", {
    get: function(){ return this._name },
    set: function(value){ this._name = value }
});

Or ES6
pretty much the same thing as the prototypal aproach

class Ab{
    constructor(n){
        this.name = n;
    }

    get name(){ return this._name },
    set name(value){ this._name = value }
}
like image 117
Thomas Avatar answered Oct 07 '22 23:10

Thomas


The set syntax binds an object property to a function to be called when there is an attempt to set that property.

You dont need to do this.name = Name; inside set function. This recursively triggers the setter causing stack overflow. In fact you dont need a setter altogether in this case. Use setter only if, you have to do some extra work when the the property is set or modified.

More

like image 4
rishabh dev Avatar answered Oct 08 '22 01:10

rishabh dev