admin管理员组

文章数量:1279019

Could you explain why I get

Uncaught RangeError: Maximum call stack size exceeded

in this example. What's the sequence of actions?

"use strict";

let myClass = class myClass{
  constructor(name){ 
    this.name = name;
  } 
  get name() { return this.name; } 
  set name(name){ this.name = name; }
}  

let myObj = new myClass("John");

Could you explain why I get

Uncaught RangeError: Maximum call stack size exceeded

in this example. What's the sequence of actions?

"use strict";

let myClass = class myClass{
  constructor(name){ 
    this.name = name;
  } 
  get name() { return this.name; } 
  set name(name){ this.name = name; }
}  

let myObj = new myClass("John");
Share Improve this question edited Feb 13, 2016 at 14:17 Alexey Galanov asked Feb 13, 2016 at 13:51 Alexey GalanovAlexey Galanov 4514 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

You're calling the setter from the setter, infinitely looping.

set name(name) {
  this.name = name; // <-- ⛔ `this.name` calls the `set`ter again
}

You should use a differently named backing variable of some sort. Unfortunately the "TC39 Private Fields" proposal for JS is not finalized so they will be public, and a naming convention is needed for now.

Here's a modern example:

class Person {
  _name = ""; // 'private' by convention, see also: https://github./tc39/proposal-class-fields#private-fields
  get name() {
    return this._name;
  }
  set name(value) {
    this._name = value;
  }

  constructor(name) {
    this.name = name;
  }
}

Or following the Question's structure:

"use strict";

let myClass = class myClass {
  constructor(name) {
    this.name = name;
  }

  get name() {
    return this._name;
  }

  set name(name) {
    this._name = name;
  }
}

let myObj = new myClass("John");

console.log(myObj);

To my surprise it's not trivial to have variables private to a class.

本文标签: javascriptFieldgetter and setter with the same nameStack Overflow