How do i read only value attr? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How do i read only value attr? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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;
  }
});
We are here to answer your question about How do i read only value attr? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji