javascript 中的私有成员真的那么复杂吗?

Are private members in javascript really that complicated?

我试图澄清我对 Javascript 中私有成员的理解。看起来应该很简单:

function MyClass(param) {
  var thisIsPrivate = param;
  this.getPrivateMember = function() {
    return thisIsPrivate;
  }
}

var thing = new MyClass('tada!');

console.log(thing.thisIsPrivate)       // undefined
console.log(thing.getPrivateMember())  // "tada!"

在我的阅读中,我不断看到一些文章甚至没有提到这个作为一个选项,而是提出了复杂的解决方案,比如使用闭包或 WeakMaps。通常得出的结论是,在 Javascript.

中没有使用私有成员的好方法

任何人都可以补充我所缺少的吗?由于某种原因,这是个坏主意吗?

感谢您的回复。所以我对这种方法的局限性的理解是在构造函数之外添加的方法看不到 'private' 变量。感觉有点豁然开朗。

function MyClass(param) {
  var thisIsPrivate = param;
  this.getPrivateMember = function() {
    return thisIsPrivate;
  }
}

MyClass.prototype.showPrivateMember = function(){
  console.log(thisIsPrivate);
}

var thing = new MyClass('tada!');

console.log(thing.thisIsPrivate)       // undefined
console.log(thing.getPrivateMember())  // tada!

thing.showPrivateMember(); // ReferenceError: thisIsPrivate is not defined at MyClass.showPrivateMember

thing.showPrivateMember = function(){
  console.log(thisIsPrivate);
}

thing.showPrivateMember(); // ReferenceError: thisIsPrivate is not defined at MyClass.showPrivateMember