snugobject is a mutable JavaScript object with an immutable hidden class.
Works in the browser. No runtime dependencies. Drop-in* replacement to objects. Get more out of your JS engine.
npm i gobj
Or import directly from a CDN:
import { Obj } from "https://cdn.jsdelivr.net/npm/gobj/+esm";
The Obj
class creates a mutable JavaScript object with an immutable hidden class, powered by a Map
and a Proxy
.
new Obj(data);
data
(object, optional): The initial data to populate the object with.
The constructor may throw:
TypeError
: Ifdata
isnull
.
import { Obj } from "gobj";
const obj = new Obj({
name: "John Doe",
age: 30,
});
console.log(obj.name); // Output: "John Doe"
console.log(obj.age); // Output: 30
obj.email = "[email protected]";
console.log(obj.email); // Output: "[email protected]"
delete obj.age; // Output: true
console.log(obj.age); // Output: undefined
console.log(Object.keys(obj)); // Output: ["name", "email"]
console.log(JSON.stringify(obj)); // Output: '{"name":"John Doe","email":"[email protected]"}'
Obj
supports all standard object operations, including property access, modification, deletion, and iteration.