Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ramirezmike/swizzle.js
https://github.com/ramirezmike/swizzle.js
Last synced: 7 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/ramirezmike/swizzle.js
- Owner: ramirezmike
- Created: 2019-11-07T04:04:30.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-11-07T04:05:11.000Z (about 5 years ago)
- Last Synced: 2024-10-28T17:29:37.755Z (about 2 months ago)
- Language: JavaScript
- Size: 1000 Bytes
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# swizzle.js
### A function that creates an object with properties that are swizzable## Installation
Include swizzle.js in your scripts
```html```
## How to use
swizzle.js creates a global function "Swizzle" with two parameters: an object and an array of properties on the object that can be swizzled. The function returns a proxied version of the object with overrides for get/set that allow swizzling any property defined in the array of properties.
```javascript
function Vector4(x, y, z, w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;this.someOtherProp = "some other prop";
// set function to return "swizzled" version of constructed object with x y z and w marked as "swizzable"
return Swizzle(this, ['x', 'y', 'z', 'w']);
}let v1 = new Vector4(1, 2, 3, 4);
let v2 = new Vector4(5, 6, 7, 8);console.log(v1.x); // 1
console.log(v1.xyz); // [1, 2, 3]
console.log(v1.xxyz); // [1, 1, 2, 3]v1.xyz = v2.xyz;
console.log(v1.xyz); // [5, 6, 7]
console.log(v1.x); // 5
console.log(v1.y); // 6
console.log(v1.z); // 7console.log(v1.someOtherProp); // "some other prop"
```