The problem is that the setters change local variables that are not obtained anywhere else.
The values ​​of the name and age fields are set once and then not changed.
For corrections, you can use getters instead of fields.
var person = (function(name, age) { var $name = name, $age = age; return { get name() { return $name }, get age() { return $age }, setName: function(name) { $name = name; }, setAge: function(age) { $age = age; } }; })('Gregory', 42); person.setName('Miller'); person.setAge(30); console.log(person.name); console.log(person.age);
Or change the object fields directly
var person = (function(name, age) { return { name: name, age: age, setName: function(name) { this.name = name; }, setAge: function(age) { this.age = age; } }; })('Gregory', 42); person.setName('Miller'); person.setAge(30); console.log(person.name); console.log(person.age);
but in this case it is worth monitoring the possible loss of the call context