chill-patch
Advanced tools
+1
-1
| { | ||
| "name": "chill-patch", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "description": "Stress-free monkey-patching", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+22
-14
@@ -0,1 +1,3 @@ | ||
| [](http://chillestmonkey.com/) | ||
| # chill-patch: Stress-free Monkey Patching for JavaScript | ||
@@ -15,18 +17,5 @@ | ||
| `chill-patch` is safe because the return value is a [Symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) and symbols are guaranteed to be unique. That means that the only way to access the new method you created is to have access to the symbol. | ||
| You can use `chill-patch` to use off-the-shelf `this`-less functions as methods: | ||
| > The only way another programmer can get access to symbols on an object in another scope is if they are [hellbent on doing so](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols), in which case they know they are going off-roading. | ||
| When you add a property to a prototype using a symbol, it's hidden, so you can safely pass off the patched object to other parts of the codebase, without other programmers knowing its there or being affected by it. | ||
| ```js | ||
| // after the above code is run, there is no change to the `ownPropertyNames` of the patched class | ||
| Object.getOwnPropertyNames(Array.prototype) // doesn't include anything new! | ||
| ``` | ||
| Another advantage of the `chill-patch` API is that you can add methods to objects without using `this`! So you can use off-the-shelf `this`-less functions without doing anything special. | ||
| ```js | ||
| const toggleSet = require('toggle-set') | ||
@@ -45,2 +34,4 @@ const set = new Set([1, 2, 3]) | ||
| > I don't 100% recommend using `chill-patch`, since [the pipeline operator](#something-better) may be coming to JavaScript, and it's much more elegant. | ||
| ## Uses | ||
@@ -84,2 +75,17 @@ | ||
| ## Why it's Safe | ||
| `chill-patch` is safe because the return value is a [Symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) and symbols are guaranteed to be unique. That means that the only way to access the new method you created is to have access to the symbol. | ||
| > The only way another programmer can get access to symbols on an object in another scope is if they are [hellbent on doing so](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols), in which case they know they are going off-roading. | ||
| When you add a property to a prototype using a symbol, it's hidden, so you can safely pass off the patched object to other parts of the codebase, without other programmers knowing its there or being affected by it. | ||
| ```js | ||
| // after the above code is run, there is no change to the `ownPropertyNames` of the patched class | ||
| Object.getOwnPropertyNames(Array.prototype) // doesn't include anything new! | ||
| ``` | ||
| ## Similar Tech in other Languages | ||
@@ -93,2 +99,4 @@ | ||
| - Nim [method call syntax](http://nim-lang.org/docs/manual.html#procedures-method-call-syntax) | ||
| ## Something Better | ||
@@ -95,0 +103,0 @@ |
5495
5.03%115
7.48%