What encapsulation pattern does this code demonstrate?
function Person(name) {
Object.defineProperty(this, 'name', {
get() { return name; },
enumerable: true
});
}
const person = new Person('John');
console.log(person.name);
person.name = 'Jane';
console.log(person.name);
This code demonstrates the private variable with privileged method pattern: 1) The 'name' parameter is captured in a closure, making it private to the instance, 2) The property accessor (getter) provides read-only access to this private data, 3) No setter is defined, making the property effectively read-only, 4) The property is enumerable, so it appears in iterations and Object.keys(), 5) External code cannot modify the name directly, but internal methods could, 6) This creates true private state in JavaScript before class private fields were available, 7) This pattern leverages closures and property descriptors to achieve encapsulation.