unreflect
v0.0.7
Published
Access and modify private class fields and methods in JavaScript
Readme
unreflect
Access and modify private class fields and methods in JavaScript using the V8 inspector API.
Installation
npx nypm install unreflectUsage
import { ReflectionClass } from "unreflect";
class User {
#name = "John";
email = "[email protected]";
getName() {
return this.#name;
}
}
const user = new User();
const ref = new ReflectionClass(user);
// Get class name
ref.getClassName(); // "User"
// Get/set public properties
await ref.getPropertyValue("email"); // "[email protected]"
ref.setProperty("email", "[email protected]");
// Get/set private properties (use # prefix)
await ref.getPropertyValue("#name"); // "John"
ref.setProperty("#name", "Jane");
user.getName(); // "Jane"
// Get property metadata
await ref.getProperty("#name");
// { name: "#name", visibility: "private", value: "Jane" }
await ref.getProperty("email");
// { name: "email", visibility: "public", value: "[email protected]" }
// List all properties
await ref.getProperties();
// [
// { name: "email", visibility: "public", value: "[email protected]" },
// { name: "#name", visibility: "private" }
// ]
// List all methods (including private)
await ref.getMethods();
// [
// { name: "getName", visibility: "public", isStatic: false }
// ]
// Check existence
await ref.hasProperty("#name"); // true
await ref.hasProperty("email"); // true
await ref.hasMethod("getName"); // trueAPI
| Method | Description |
| -------------------------- | --------------------------------------------------- |
| getClassName() | Returns the class name |
| setProperty(name, value) | Sets a property value (use # prefix for private) |
| getProperty(name) | Returns property metadata with visibility and value |
| getPropertyValue(name) | Returns just the property value |
| getProperties() | Returns all properties with visibility |
| getMethods() | Returns all methods with visibility and static flag |
| hasProperty(name) | Checks if a property exists |
| hasMethod(name) | Checks if a method exists |
License
Published under the MIT license.
