Table of Contents generated with DocToc
_R is a reflection library.
npm install reflect-r
Node.js:
var _R = require('reflect-r');
Browsers:
<script src="./reflect-helpers.js"></script>
gulp test
gulp build
- Statements : 91.1% (215/236)
- Branches : 70.41% (69/98)
- Functions : 88.68% (47/53)
- Lines : 90.99% (212/233)
_R.$setDirective(directive)
Changes directive placed in every function.
_R.DIRECTIVE_NORMAL // ''; is placed before every new function
_R.DIRECTIVE_STRICT // 'use strict'; is placed before every function; default
_R.DIRECTIVE_ASM // 'use asm'; is placed before every new function
_R.isValidVariableName
tests variable name against current implementation rules._R.isBoundOrNativeFunction
is slow in V8 (due to V8's incompatibility with ES6 spec).
_R.isValidVariableName(name)
Checks if supplied name
is a valid variable identifier in current JS implementation.
_R.isBoundOrNativeFunction(func)
Checks if supplied func
is bound (.bind
) or native code.
_R.getFunctionSourceCode(func)
Returns FunctionExpression
. Throws error when called on non-function, bound function or native-code function.
_R.getInternalClass(what)
Returns [[Class]]
's name of what
.
_R.getObjectPrototype(what)
If what
is an object, returns it's prototype. Otherwise, returns null
.
Can return invalid object in IE8 and lower.
_R.getPrototypesChain(what)
If what
is an object, returns array containing what
and objects in it's prototype chain (array ends with null
).
Otherwise, return [what, null]
.
_R.declosureFunction(func[, transformer]);
Returns func
redefined in global context. transformer
function is called on source code of func
before code evaluation.
function transformer(sourceCode, originalFunction) { /* magic */ return transformedSourceCode; }
_R.createNamedFunction(name, [...argsNames[, sourceCode]])
Works like Function
constructor but first argument is the function name (used in recursive calls).
_R.createClosure(func, context, name)
Creates closure in given context. Example:
console.log(window.$) // undefined
var showFactorial = _R.createClosure(
function (n) {
if (!n || n === 1)
return 1;
var curr = n*factorial(n-1);
$('div.factorial').html(curr);
return curr;
},
{
$: jQuery
},
'factorial'
);
showFactorial(5);
_R.makeGeneric(func);
Changes method to function accepting this
as first argument.
var slice = _R.makeGeneric([].slice);
slice(arguments, 2, 7);
_R.indirectEval(code[, preparationCode]);
Works like eval
but:
- code scope and context always follow rules for ECMAScript strict mode
eval
- if preparationCode isn't supplied, code is run with global settings directive (default: 'use strict')
_R.construct(constructor, args)
This function follows specification of Reflect.construct
from ES6 (26.1.2).
_R.construct(Date, [30,3,1990]);
_R.has(obj, key)
This function follows specification of Reflect.has
from ES6 (26.1.9).
_R.apply(function, target, args);
This function follows specification of Reflect.apply
from ES6 (26.1.3).
new _R.Proxy(target, getHandler, setHandler);
Creates proxy object for target. Proxy objects are sealed (Object.seal
).
_R.createProxy(target, getHandler, setHandler);
Alias for new _R.Proxy
.
_R.defaultSetter
Transfers assigment to original object.
function Circle(r) {
'strict mode';
this.diameter = undefined; // property have to exist
this.area = undefined; // property have to exist
this.radius = r;
return _R.createProxy(this, Circle.getter, Circle.setter);
}
Circle.getter = function circleGetter(originalObject, proxyObject, propertyName) {
if (propertyName === 'radius') {
return originalObject.radius;
}
if (propertyName === 'diameter') {
return proxyObject.radius*2;
}
if (propertyName === 'area') {
return proxyObject.radius*proxyObject.radius*Math.PI;
}
};
Circle.setter = function circleSetter(originalObject, proxyObject, propertyName, propertyValue) {
if (propertyName !== 'radius') {
throw Error('You can not modify anything in circle except radius');
}
else {
return originalObject.radius = propertyValue;
}
};
var k = new Circle(5);
k.radius === k.diameter*2; // true
k.diameter = 7; // Always throw Error: You can not modify anything in circle except radius
k.oh = 'hai'; // Error in strict mode; Does nothing in sloppy mode
k.radius = 11; // works
console.log(k.diameter); // 22
_R.forbidPropertyNames(what, ...names)
Forbids writing to properties of object.
Returns modified what
.
...names
can be any combination of strings and array of strings.
_R.forbidPropertyNames(Object.prototype, ['get', 'set'], 'enumerable', ['writable', 'configurable']);
Object.prototype.get = function(){}; // throws TypeError
_R.addMagicLengthProperty(what [,readOnly=true]);
Adds magic length
property to what
object. It works almost as length
property of arrays but it does not support length exceeding maximal index.
If readOnly
argument is false, changes in length
property will remove indexes over specified length.