npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

private-field

v1.0.2

Published

Allows you to define native private fields on arbitrary objects

Downloads

4

Readme

private-field

Allows you to define native private fields on arbitrary objects

createPrivateField()

Crates a class with some static methods that allow you to define a private field on anything

Static methods

has()

Tells whether the private field is defined in the provided object

get()

Gets the value of the private field in the provided object

set()

Sets the value of the private field in the provided object

define()

Defines the private field on the provided object

Usage

const field = createPrivateField(12);
const obj = {};
console.log(field.has(obj));              // → false
console.log(field.define(obj) === obj);   // → true
console.log(field.has(obj));              // → true
console.log(field.get(obj));              // → 12
console.log(field.set(obj, 14));          // → 14
console.log(field.get(obj));              // → 14

Unfortunately, you can't use a private field until you define it

const field = createPrivateField();
const obj = {};
field.get(obj);                           // Error: The property is not defined on the object
field.set(obj);                           // Error: Same thing, even when setting it
field.has(obj);                           // Ok: Checking always works

You can create multiple private fields using the same function and they will all be indipendent from each other

const a = createPrivateField(1);
const b = createPrivateField("string");
const obj = {};
a.define(obj);
b.define(obj);
console.log(a.get(obj));                  // → 1
console.log(b.get(obj));                  // → "string"

If you define a private field on a Proxy, it won't be defined on the target, but on the Proxy itself (This is standard behaviour by the way). You can see it clearly in the DevTools

const field = createPrivateField();
const target = {}, handler = {};
const proxy = new Proxy(target, handler);
field.define(proxy);
console.log(field.has(target));           // → false
console.log(field.has(handler));          // → false
console.log(field.has(proxy));            // → true

The function has multiple overloads that infer the type of the field:

createPrivateField();                     // The field type is `unknown`
createPrivateField(1);                    // The field type is `number`
createPrivateField(1 as const);           // The field type is `1`
createPrivateField("string");             // The field type is `string`
createPrivateField<boolean>();            // The field type is `boolean | undefined` since I didn't pass the initial value

Identity

Utility class that returns whatever has been passed as the first argument of its constructor. You can use it to define your own attached private properties

class AttachedFields extends Identity {
    #field1 = 1;
    #field2 = 2;
    #field3 = 3;
    // ...

    // Methods or accessors that can access those private fields
}

const obj = {};
AttachedFields.define(obj);               // The `define()` static method is on the base class
console.log(obj);                         // → { #field1: 1, #field2: 2, #field3: 3 }