property-definer
v1.0.8
Published
Module based on `Object.defineProperty`
Downloads
2
Readme
PropertyDefiner
Module based on Object.defineProperty
;
Install
npm install property-definer
;
Import
import define, { defineValue, defineAccessor, bindDefineValue, bindDefineAccessor, UNCONFIGURABLE, UNENUMERABLE, UNWRITABLE } from 'property-definer'
Description
Here are some enumerations that can generate descriptor
more quickly.
UNCONFIGURABLE
(value:1
) means{ configurable: false }
;UNENUMERABLE
(value:2
) means{ enumerable: false }
;UNWRITABLE
(value:4
) means{ writable: false }
;
You can generate complete descriptor
by mixing these enumerations.
import { UNCONFIGURABLE, UNENUMERABLE } from "property-definer"; const descriptor = UNCONFIGURABLE | UNENUMERABLE; // description = 1 | 2; const Joker = defineValue({}, "name", "Joker", descriptor); // "{\"name\":{\"value\":\"Joker\",\"writable\":false,\"enumerable\":false,\"configurable\":true}}" console.log(JSON.stringify(Object.getOwnPropertyDescriptors(Joker)));
- You can use
UNCONFIGURABLE + UNENUMERABLE
instead ofUNCONFIGURABLE | UNENUMERABLE
, because they have equal values; - But you can’t use
UNCONFIGURABLE + UNCONFIGURABLE
instead ofUNCONFIGURABLE | UNCONFIGURABLE
;
Here we go
define
Define properties in bulk
function define(target: object, defines: object, description?: number | string): T;
The define
method needs to accept 2~3 arguments,
- arguments[0]:
target
Object on which to add or modify the property; - arguments[1]:
defines
JavaScript object that contains one or more descriptor objects. Each descriptor object describes a data property or an accessor property; - arguments[2]:
description
(Optional) Can be a number or a string, See thedescription
section above to learn more;
import define from 'property-definer'; var Joker = {}; define(Joker, { name: "Joker", gender: { get(){ return "male" } }, doSomething: { value: "Commit a crime" } }, 0); console.log({...Joker}); // { name: "Joker", gender: "male", doSomething: "Commit a crime" }
defineValue
Something like Object.defineProperty(target, key, { value: "some value" })
;
function defineValue(target: object, key: PropertyKey, value: any, description?: number | string): object;
The defineValue
method needs to accept 3~4 arguments,
- arguments[0]:
target
Object on which to add or modify the property; - arguments[1]:
key
The property name, expectstring
/number
/symbol
; - arguments[2]:
value
Value oftarget
[key
]; - arguments[3]:
description
(Optional) Can be a number or a string, See thedescription
section above to learn more;
/** * @description You can also use: * @example * import { define } from 'property-definer'; * const { defineValue } = define; */ import { defineValue } from 'property-definer'; var Joker = {}; defineValue(Joker, "name", "Joker", UNCONFIGURABLE); defineValue(Joker, "gender", "male", UNENUMERABLE); defineValue(Joker, "doSomething", "Commit a crime", UNWRITABLE); Joker.doSomething = "Be a friendly Gotham citizen"; // Will not take effect console.log({...Joker}); // { name: "Joker", doSomething: "Commit a crime" }
defineAccessor
Something like `Object.defineProperty(target, key, { get(){ return "some thing" }, set(value){ /** do something */ } })
function defineValue(target: object, key: PropertyKey, accessor: { get?(): any; set?:(value: any): void; }, description?: number | string): object;
The defineAccessor
method needs to accept 3~4 arguments,
- arguments[0]:
target
Object on which to add or modify the property; - arguments[1]:
key
The property name, expectstring
/number
/symbol
; - arguments[2]:
accessor
Object containingGetter
orSetter
; - arguments[3]:
description
(Optional) Can be a number or a string, See thedescription
section above to learn more;
/** * @description You can also use: * @example * import { define } from 'property-definer'; * const { defineAccessor } = define; */ import { defineAccessor } from 'property-definer'; var Joker = {}; defineValue(Joker, "name", { get(){ return "Joker" } }, UNCONFIGURABLE); defineValue(Joker, "gender", { get(){ return "male" } }, UNENUMERABLE); console.log({...Joker}); // { name: "Joker", gender: "male" }
bindDefineValue
Bind the descriptor
, and can also be further bound to the target
and key
function bindDefineValue(description: number | string): (target: object, key: PropertyKey, value: any) => object; function bindDefineValue(description: number | string, target: object): (key: PropertyKey, value: any) => object; function bindDefineValue(description: number | string, target: object, key: PropertyKey): (value: any) => object;
The bindDefineValue
method needs to accept 1~3 arguments,
- arguments[0]:
description
Can be a number or a string, See thedescription
section above to learn more; - arguments[1]:
target
(Optional) Object on which to add or modify the property; - arguments[2]:
key
(Optional) The property name, expectstring
/number
/symbol
;
/** * @description You can also use: * @example * import { define } from 'property-definer'; * const { bindDefineValue } = define; */ import { bindDefineValue } from 'property-definer'; var Joker = {}; const bound = bindDefineValue(UNCONFIGURABLE | UNENUMERABLE | UNWRITABLE); const boundTarget = bindDefineValue(UNCONFIGURABLE | UNENUMERABLE | UNWRITABLE, Joker); const boundTargetAndKey = bindDefineValue(UNCONFIGURABLE | UNENUMERABLE | UNWRITABLE, Joker, "name"); boundTargetAndKey("Joker"); boundTarget("gender", "male"); bound(Joker, "doSomething", "Commit a crime"); console.log({...Joker}); // { name: "Joker", gender: "male", doSomething: "Commit a crime" }
bindDefineAccessor
Bind the descriptor
, and can also be further bound to the target
and key
function bindDefineAccessor(description: number | string): (target: object, key: PropertyKey, accessor: { get?(): any; set?:(value: any): void; }) => object; function bindDefineAccessor(description: number | string, target: object): (key: PropertyKey, accessor: { get?(): any; set?:(value: any): void; }) => object; function bindDefineAccessor(description: number | string, target: object, key: PropertyKey): (accessor: { get?(): any; set?:(value: any): void; }) => object;
The bindDefineValue
method needs to accept 1~3 arguments,
- arguments[0]:
description
Can be a number or a string, See thedescription
section above to learn more; - arguments[1]:
target
(Optional) Object on which to add or modify the property; - arguments[2]:
key
(Optional) The property name, expectstring
/number
/symbol
;
/** * @description You can also use: * @example * import { define } from 'property-definer'; * const { bindDefineAccessor } = define; */ import { bindDefineAccessor } from 'property-definer'; var Joker = {}; const bound = bindDefineAccessor(UNCONFIGURABLE | UNENUMERABLE); const boundTarget = bindDefineAccessor(UNCONFIGURABLE | UNENUMERABLE, Joker); const boundTargetAndKey = bindDefineAccessor(UNCONFIGURABLE | UNENUMERABLE, Joker, "name"); boundTargetAndKey({ get(){ return "Joker" } }); boundTarget("gender", { get(){ return "male" } }); bound(Joker, "doSomething", { get(){ return "Commit a crime" } }); console.log({...Joker}); // { name: "Joker", gender: "male", doSomething: "Commit a crime" }