Code Monkey home page Code Monkey logo

define-accessor-property's Introduction

define-accessor-property Version Badge

github actions coverage License Downloads

npm badge

Define an accessor property on an object. In an engine without descriptors, in loose mode, when only a getter is provided, nonEnumerable is false, and nonConfigurable is false, wil fall back to assignment - otherwise, it will throw.

The two non* options can also be passed null, which will use the existing state if available.

The loose option will mean that if you attempt to set a nonconfigurable/nonwritable accessor property with set, in an environment without descriptor support, it will fall back to normal assignment (and eagerly evaluate the getter).

Usage

var defineAccessorProperty = require('define-accessor-property');
var assert = require('assert');

var str = 'value';
var strThunk = function () { return str; };
var strSetter = function (v) { str = v; };
var random = function () { return Math.random(); };

var obj = {};
defineAccessorProperty(
	obj,
	'key',
	{
		get: strThunk,
		set: strSetter,
	}
);
defineAccessorProperty(
	obj,
	'key2',
	{
		get: random, // at least one of "get" or "set" must be provided
		nonConfigurable: true, // optional
		nonEnumerable: true, // optional
		loose: false, // optional
	}
);

assert.deepEqual(
	Object.getOwnPropertyDescriptors(obj),
	{
		key: {
			configurable: true,
			enumerable: true,
			get: strThunk,
			set: strSetter,
		},
		key2: {
			configurable: false,
			enumerable: false,
			get: random,
			set: undefined,
		},
	}
);

define-accessor-property's People

Contributors

ljharb avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

define-accessor-property'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 all non-major dependencies (@ljharb/tsconfig, eslint)
  • [Deps] Update Update eslint to v9
  • [Deps] Update Update nyc to v15
  • ๐Ÿ” Create all pending approval PRs at once ๐Ÿ”

Detected dependencies

github-actions
.github/workflows/node-aught.yml
.github/workflows/node-pretest.yml
.github/workflows/node-tens.yml
.github/workflows/rebase.yml
.github/workflows/require-allow-edits.yml
npm
package.json
  • es-define-property ^1.0.0
  • es-errors ^1.3.0
  • gopd ^1.0.1
  • @ljharb/eslint-config ^21.1.0
  • @ljharb/tsconfig ^0.1.1
  • @types/call-bind ^1.0.5
  • @types/define-properties ^1.1.5
  • @types/es-value-fixtures ^1.4.4
  • @types/for-each ^0.3.3
  • @types/gopd ^1.0.3
  • @types/has ^1.0.2
  • @types/has-property-descriptors ^1.0.3
  • @types/object-inspect ^1.8.4
  • @types/object.assign ^4.1.0
  • @types/object.getownpropertydescriptors ^2.1.4
  • @types/tape ^5.6.4
  • aud ^2.0.4
  • auto-changelog ^2.4.0
  • es-value-fixtures ^1.4.2
  • eslint =8.8.0
  • evalmd ^0.0.19
  • for-each ^0.3.3
  • has-property-descriptors ^1.0.2
  • in-publish ^2.0.1
  • npmignore ^0.3.1
  • nyc ^10.3.2
  • object-inspect ^1.13.1
  • object.assign ^4.1.5
  • object.getownpropertydescriptors ^2.1.7
  • safe-publish-latest ^2.0.0
  • tape ^5.7.5
  • node >= 0.4

  • 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.