Module based on `Object.defineProperty`
npm install property-definerModule based on Object.defineProperty;
> npm install property-definer;
> ``typescript`
> import
> define, {
> defineValue,
> defineAccessor,
> bindDefineValue,
> bindDefineAccessor,
> UNCONFIGURABLE,
> UNENUMERABLE,
> UNWRITABLE
> }
> from 'property-definer'
>
Here are some enumerations that can generate descriptor more quickly.
1. UNCONFIGURABLE (value: 1) means { configurable: false };UNENUMERABLE
2. (value: 2) means { enumerable: false };UNWRITABLE
3. (value: 4) means { writable: false };
You can generate complete descriptor by mixing these enumerations.
> `typescript`
> 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)));
>
1. You can use UNCONFIGURABLE + UNENUMERABLE instead of UNCONFIGURABLE | UNENUMERABLE, because they have equal values;UNCONFIGURABLE + UNCONFIGURABLE
2. But you can’t use instead of UNCONFIGURABLE | UNCONFIGURABLE;
Define properties in bulk
> `typescript`
> function define(target: object, defines: object, description?: number | string): T;
>
The define method needs to accept 2~3 arguments,
1. arguments[0]: target Object on which to add or modify the property;defines
2. arguments[1]: JavaScript object that contains one or more descriptor objects. Each descriptor object describes a data property or an accessor property;description
3. arguments[2]: (Optional) Can be a number or a string, See the description section above to learn more;
> `typescript`
> 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" }
>
Something like Object.defineProperty(target, key, { value: "some value" });
> `typescript`
> function defineValue(target: object, key: PropertyKey, value: any, description?: number | string): object;
>
The defineValue method needs to accept 3~4 arguments,
1. arguments[0]: target Object on which to add or modify the property;key
2. arguments[1]: The property name, expect string / number / symbol;value
3. arguments[2]: Value of target[key];description
4. arguments[3]: (Optional) Can be a number or a string, See the description section above to learn more;
> `typescript`
> /**
> * @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" }
>
Something like Object.defineProperty(target, key, { get(){ return "some thing" }, set(value){ /* do something / } })
> ``typescript`
> 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,
1. arguments[0]: target Object on which to add or modify the property;key
2. arguments[1]: The property name, expect string / number / symbol;accessor
3. arguments[2]: Object containing Getter or Setter;description
4. arguments[3]: (Optional) Can be a number or a string, See the description section above to learn more;
> `typescript`
> /**
> * @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" }
>
Bind the descriptor, and can also be further bound to the target and key
> `typescript`
> 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,
1. arguments[0]: description Can be a number or a string, See the description section above to learn more;target
2. arguments[1]: (Optional) Object on which to add or modify the property;key
3. arguments[2]: (Optional) The property name, expect string / number / symbol;
> `typescript`
> /**
> * @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" }
>
Bind the descriptor, and can also be further bound to the target and key
> `typescript`
> 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,
1. arguments[0]: description Can be a number or a string, See the description section above to learn more;target
2. arguments[1]: (Optional) Object on which to add or modify the property;key
3. arguments[2]: (Optional) The property name, expect string / number / symbol;
> `typescript``
> /**
> * @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" }
>