GithubHelp home page GithubHelp logo

private-fields's Introduction

private-fields Version Badge

dependency status dev dependency status License Downloads

npm badge

What private fields, methods, or accessors does this class have?

โš ๏ธ ๐Ÿšจ โš ๏ธ DISCLAIMER This capability - observing the existence, name, and in many cases, contents of class private fields/methods/accessors - is not supposed to exist. The inability to in any way interact with private fields is a critical part of their design in the JavaScript language, and is the only thing that makes them truly "private".

This is possible because node, in v12.5+ as of this writing, directly exposes the v8 engine's internal debugger protocol via the core inspector module. This should not be exposed to runtime code - it's meant for Chrome's devtools to be able to use, for debugging only. It will not work in any other environment.

Please do not use this for any non-debugging purpose. Private fields, methods, and accessors, just like closed-over variables, variable names, or function argument names, are an internal implementation detail, and they can and likely will be changed in a non-semver-major update, because by definition they are not breaking changes - since they're not observable. Consider yourself warned.

Note: this package will work down to node v8.3, but private fields support was added in node v12.0, and private methods/accessors were added in node v14.6.

Getting started

npm install --save private-fields

Usage/Examples

import getPrivateFields from 'private-fields';
import assert from 'assert';

const objectValue = { a: 1 };
const symbolValue = Symbol.iterator;
class C {
	#x;
	#y = 3;
	#foo() {}
	get #z() {}
	set #z(v) {}

	#object = objectValue;
	#symbol = symbolValue;
}

const fields = await getPrivateFields(new C());
assert.deepEqual(fields, [
	{
		name: '#foo',
		type: 'function',
		description: '#foo() {}', // functions are not provided, but their toString is
	},
	{
		name: '#z',
		get: { type: 'function', description: 'get #z() {}' },
		set: { type: 'function', description: 'set #z(v) {}' },
	},
	{
		name: '#x',
		value: undefined,
	},
	{
		name: '#y',
		value: 3,
	},
	{
		name: '#object',
		clonedValue: { a: 1 },
	},
	{
		name: '#symbol',
		clonedValue: Symbol(Symbol.iterator),
	},
]);

const [, , , , object, symbol] = fields;

assert.notEqual(object, objectValue); // the original object is not provided
assert.deepEqual(object, objectValue); // but its properties are cloned

assert.notEqual(symbol.clonedValue, Symbol.iterator); // symbols are not provided
assert.equal(symbol.description, Symbol.iterator.description); // but a symbol with the same description is

Tests

Simply clone the repo, npm install, and run npm test

private-fields's People

Contributors

ljharb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

targos

private-fields's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Pending Approval

These branches will be created by Renovate only once you click their checkbox below.

  • [Deps] Update Update eslint to v8.56.0
  • [Deps] Update Update actions/checkout action to v4
  • ๐Ÿ” Create all pending approval PRs at once ๐Ÿ”

Detected dependencies

github-actions
.github/workflows/node-esm.yml
  • actions/checkout v2
.github/workflows/node-pretest.yml
  • actions/checkout v2
  • actions/checkout v2
.github/workflows/node.yml
  • actions/checkout v2
  • actions/checkout v2
.github/workflows/rebase.yml
  • actions/checkout v3
.github/workflows/require-allow-edits.yml
npm
package.json
  • has-private-fields ^1.1.0
  • hasown ^2.0.0
  • object-inspect ^1.13.1
  • @ljharb/eslint-config ^21.1.0
  • aud ^2.0.3
  • auto-changelog ^2.4.0
  • es-value-fixtures ^1.4.2
  • eslint =8.8.0
  • eslint-plugin-node ^11.1.0
  • npmignore ^0.3.0
  • nyc ^15.1.0
  • symbol.prototype.description ^1.0.5
  • tape ^5.7.2
  • node >= 8.3

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.