rmprop
Override all properties inhereted by an object's prototype chain
If you've ever wanted to use property names for other purposes such as the .length
property on an Array
or String
, this library will transform your object (eg: an instance of a class) into an object with all properties found in its prototype chain set to undefined
, effectively stopping the javascript engine from bubbling up the prototype chain to find the values for those properties.
This module was created to support meta-programming in javascript; it is useful for creating API objects in which a property name is set dynamically and used like a dictionary (ie: when the property name could be any string). While Proxies are not yet supported, this might suffice as an alternative to your trap needs.
Install
$ npm install --save rmprop
Usage
rmprop(thing: anytype[, exclude: array{string}])
Finds all properties defined by each class in thing
's prototype chain, then creates a virtual copy of thing
object with all those properties set to undefined
. Not only does this block the prototype properties from being accessed, but it also lets you define properties that the original object might not allow (such as the .length
property). The optional exclude
argument declares which properties not to override.
var rmprop = ; var arr = ;// ^^ override all Array prototype properties except for the .indexOf method arrforEach; // undefined arr0; // 'hello'// ^^ this property is defined on the object itself, not in Array.prototype arrlength; // undefined// ^^ although this property is defined on the object itself, Array.prototype also contains a .length property, so this is overridden by default arr; // 1arrjoin; // undefinedarrreverse; // undefined arrprototype; // undefinedarr__proto__; // undefined
rmprop.real
This is a Symbol which allows you to access the original (real) value of thing
that was given to rmprop. Eg:
var rmprop = ; var arr = ; // redefine how length is computedObject arr0; // 'hi'arrlength; // 7arrindexOf; // undefined arrrmpropreallength; // 2arrrmproprealindexOf; // function indexOf() { [native code] }
rmprop.emulateArray(arr: array, cover: object)
Special method for copying all methods from array arr
onto object cover
. Useful for creating an array that can also be a function:
let a = 'a' 'b' 'c';let f = rmprop; ; // 'hello you!'flength; // 3f0; // 'a'f; // 1 // array can be mutatedf;flength; // 4f3; // 'd' // length updates array as wellflength = 0;f;flength; // 1f0; // 'a'
Testing equality
rmprop
creates a virtual copy of the real object, meaning that it creates a property/method on a new object for each corresponding property/method on the original object. The object you get back from rmprop will never be identical to the one you gave it.
Consider the following example:
var obj = test: 'hi';var virtual = ; virtualtest; // 'hi' virtual === obj; // false virtualunpropreal === obj; // true
However, excluding the valueOf
property on primitive datatypes will yield true when the loose equality operator is used:
var num = ; num == 5; // truenum === 5; // false typeof num; // 'object'
Transparent properties/methods
The virtual copy returned by rmprop
keeps any properties not defined in thing
's prototype chain and treats them as transparent proxies for the original object. This means that changes to the virtual copy will update the original object.
var obj = test: 'hi';var virtual = ; // change the value of a property on the virtual objectvirtualtest = 'hello'; // changes are reflected by real objectobjtest; // 'hello'virtualtest; // 'hello'
Primitive datatypes
Primitive datatypes get boxed into their object equivalent (eg: 25
becomes Number(25)
) so that they can hold properties.
var str = ; // do not override .valueOf str == 'hi'; // true// ^^ loose equality operator uses valueOf function str === 'hi'; // false// ^^ strict equality checks types too; str will never be primitive type strrmpropreal === 'hi'; // true// ^^ using the .real symbol grants access to the original (real) value strlength; // undefinedstrindexOf; // undefinedstr__proto__; // undefined str instanceof String; // false
License
ISC © Blake Regalia