简体   繁体   中英

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; } });

You must add "writebale" key; Like this;

 Object.defineProperty(obj, "name", {
        value: "karl",
        writable: false
    });

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; } });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM