|
1 | 1 | # *Deprecated and not maintained anymore* |
2 | 2 |
|
3 | | -This functionality can be done with plain ES20xx. Check Jon Randy's [metho project](https://github.com/jonrandy/metho) |
4 | | -if you want/need to keep using it. |
5 | | - |
6 | | -# ProtoXT - A new way to extend Object prototypes |
7 | | - |
8 | | -A small library to allow you to safely add 'dynamic properties' to (the prototype of) objects, |
9 | | -with the help of Symbols. |
10 | | - |
11 | | -This may be useful for 'monkey patching' native JavaScript types to give them new capabilities. |
12 | | - |
13 | | -***Note***: this library initially was a fork from the [metho project](https://github.com/jonrandy/metho) (Jon Randy). |
14 | | - |
15 | | -## How to use |
16 | | - |
17 | | -The script can be imported from https://kooiinc.github.io/ProtoXT/protoxt.js |
18 | | - |
19 | | -ProtoXT is fairly simple. It offers either one function (default) or two basic functions. |
20 | | -The default or first function (`add`) adds 'dynamic properties' to target object(s) |
21 | | -(as a symbol). The non default `addMethod ` createts an object with an enclosed function |
22 | | -and properties that can later be added to Objects using a `To`-method. |
23 | | - |
24 | | -The `add`/default function will return a `Symbol`. |
25 | | - |
26 | | -The `addMethod` function returns an Object containing the `To` method to add an enclosed function |
27 | | -to one or more Objects (prototypes) later. The `To` method returns a `Symbol` too. |
28 | | - |
29 | | -The resulting `Symbol`s are the property 'names'. |
30 | | - |
31 | | -## Using the default |
32 | | -Import the library: |
33 | | - |
34 | | -`import addMethod from [location of protox.js]`. |
35 | | - |
36 | | -Now `addMethod is available` |
37 | | -`addMethod(targetOrTargets, function, [{outerSyntax: [true/false], symbolName: [a name]}])` |
38 | | - |
39 | | -When the default is used (let's call it `addMethod`) it is the only function you'll need. |
40 | | - |
41 | | -It will use `addWithParams` or `addSimple` based on the arity (the number of parameters) |
42 | | -of the passed function. An arity of 0 will cause `addSimple` to be used <sup><b>*</b>)</sup>. |
43 | | -Anything else will cause `addWithParams` (`outerSyntax: false`) or `addProperty` (`outerSyntax: true`) |
44 | | -to be used. |
45 | | - |
46 | | -## Using addExtensionFn |
47 | | -Import the library: |
48 | | - |
49 | | -`import {add, addExtensionFn: addMethod} from [location of protox.js]`. |
50 | | - |
51 | | -Now the methods `add` and `addMethod` are available. The `add`-method is the same as the default. |
52 | | - |
53 | | -`addMethod(function, [{outerSyntax: [true/false], symbolName: [a name]}])` |
54 | | - |
55 | | -This delivers an Object with one method: `To` which enables you to add the enclosed function as |
56 | | -(symbolic) extension to one ore more Objects (prototypes). For example: |
57 | | - |
58 | | -```js |
59 | | - const logger = addMethod(function() { console.log(this, this.contructor); }) |
60 | | - // [... code cntd] |
61 | | - const logCtor = logger.To(Array, RegExp, String); |
62 | | - /[a-z]/g[logCtor] // => /[a-z]/g, f RegeXp() [...] |
63 | | - `hello`[logCtor] // => `hello`, f String() [...] |
64 | | -``` |
65 | | - |
66 | | -When added with option `outerSyntax` set to `true` the syntax for your property will be |
67 | | -that of a more regular function call: |
68 | | -```js |
69 | | -// options.outerSyntax = true |
70 | | -object[property](x) |
71 | | -``` |
72 | | -Otherwise you use |
73 | | -```js |
74 | | -// options.outerSyntax = false |
75 | | -object[property(x)] |
76 | | -``` |
77 | | -There is a slight performance hit when not using `outerSyntax` - hence the reason for the switch. |
78 | | -To specify more than one target for the function, you should pass an array of targets. |
79 | | - |
80 | | -## Some examples: |
81 | | - |
82 | | -```js |
83 | | - import addMethod from 'protoxt'; |
84 | | - |
85 | | - // number to hexadecimal |
86 | | - const asHex = addMethod( |
87 | | - Number, |
88 | | - function () { |
89 | | - return this.toString(16); |
90 | | - } |
91 | | - ); |
92 | | - console.log(65534[asHex]); // fffe |
93 | | - |
94 | | - // string to uppercase |
95 | | - const upper = addMethod( |
96 | | - String, |
97 | | - function () { |
98 | | - return this.toUpperCase(); |
99 | | - } |
100 | | - ); |
101 | | - |
102 | | - // Note the default [length] value here |
103 | | - const chunk = addMethod( |
104 | | - String, |
105 | | - function (length = 2) { |
106 | | - return this.match(new RegExp('.{1,' + length + '}', 'g')) |
107 | | - } |
108 | | - ); |
109 | | - |
110 | | - console.log("Hello World!"[upper][chunk(2)]); // ['HE', 'LL', 'O ', 'WO', 'RL', 'D!'] |
111 | | - |
112 | | - const dateFormatter = () => { |
113 | | - const dateOnlyOptions = { |
114 | | - year: 'numeric', |
115 | | - month: 'long', |
116 | | - day: 'numeric', |
117 | | - timeZone: 'Europe/Amsterdam', }; |
118 | | - return addMethod(Date, function() { |
119 | | - return new Intl.DateTimeFormat(`nl-NL`, dateOnlyOptions).format(this); |
120 | | - }); |
121 | | - }; |
122 | | - |
123 | | - const format = dateFormatter(); |
124 | | - |
125 | | - console.log(new Date(`2029/03/02`)[format]); // 2 maart 2029 |
126 | | -``` |
127 | | - |
128 | | -[**See also**](https://stackblitz.com/edit/web-platform-atdytt?file=script.js) |
129 | | - |
130 | | -<sup><b>*)</b></sup> With default parameter values or spreaded arguments the arity |
131 | | - of a function may be 0. So this fork checks length, *and* uses a (ProtoXT Function extension) `length` method |
132 | | - (`hasArgs`) to check if any such parameters with default parameters exist. |
| 3 | +This functionality can be done with [plain ES20xx](https://stackblitz.com/edit/js-jy7yngdd?file=SymbolicExtensionFactory.js). |
| 4 | +Check Jon Randy's [metho project](https://github.com/jonrandy/metho) if you want/need to keep using it. |
0 commit comments