How do i read only value attr?

I want make value attribute to read-only and i do these code but not work ??

Need help ?

const obj = {
  name: "karl"
}
const origName = obj.name;
Object.defineProperty(obj, 'name', {
  enumerable: false,
  configurable: false,
  get() {
    return origName + 2;
  }
});

Answer

You could instead use the writable property of the property descriptor, which prevents the need for a get accessor:

Object.defineProperty(obj, 'name', {
  value: "karl",
  writable: false
  get() {
    return origName + 2;
  }
});

Leave a Reply

Your email address will not be published. Required fields are marked *