js-reflection

Javascript reflection is a library to examine, introspect, and modify javascript code structure and behavior at runtime.

Usage no npm install needed!

<script type="module">
  import jsReflection from 'https://cdn.skypack.dev/js-reflection';
</script>

README

ReflectionJS

Javascript reflection is a library to examine, introspect, and modify javascript code structure and behavior at runtime.

Installation

npm install js-reflection

Usage

const reflection = require('js-reflection');

function MyObject(param) {

    this.hello = 'Hello World !';

    this.sub = {
        sayHello: function () {
            return 'Hi People !';
        }
    };
}

MyObject.prototype.test = 'test';

MyObject.prototype.sayHello = function (/* A comment */ param1, /* Another comment */ param2 /* A post comment */,
    // a line comment
    param3,
    param4) {

    return this.hello;
};

const myObject = new MyObject();

console.log('hasProperty', new reflection.Obj(myObject).hasProperty('hello'));
console.log('hasMethod', new reflection.Obj(myObject).hasMethod('sayHello'));
console.log('getName', new reflection.Obj(myObject).getName());
console.log('getConstructor', new reflection.Obj(myObject).getConstructor());
console.log('getConstructorParameters', new reflection.Obj(myObject).getConstructorParameters());
console.log('getMethod', new reflection.Obj(myObject).getMethod('sayHello'));
console.log('getMethods', new reflection.Obj(myObject).getMethods());
console.log('getMethodParameters', new reflection.Obj(myObject).getMethodParameters('sayHello'));
console.log('getProperty', new reflection.Obj(myObject).getProperty('hello'));
console.log('getProperties', new reflection.Obj(myObject).getProperties());

function myFunction(/* A comment */ param1, /* Another comment */ param2 /* A post comment */,
    // a line comment
    param3,
    param4) {}

console.log('getName', new reflection.Func(myFunction).getName());
console.log('getParameters', new reflection.Func(myFunction).getParameters());

Documentation

reflection

Javascript reflection is a library to examine, introspect, and modify javascript code structure and behavior at runtime.

reflection.typeOf(obj) ⇒ String

Get the type name of an object.

Kind: static method of reflection
Returns: String - The type name, eg 'object', 'number', 'null', 'undefined', 'regexp', 'array', 'string', 'boolean', 'function', 'date' or 'error'.

Param Type Description
obj Object Object to get the type of.

Example

const reflection = require('js-reflection');

reflection.typeOf(null);				//'null'
reflection.typeOf(undefined);		//'undefined'
reflection.typeOf(/\s/g);			//'regexp'
reflection.typeOf(true);				//'boolean'
reflection.typeOf([]);				//'array'
reflection.typeOf(1);				//'number'
reflection.typeOf('hello');			//'string'
reflection.typeOf(new Date());		//'date'
reflection.typeOf(new Error());		//'error'
reflection.typeOf(function() {});	//'function'

reflection.Obj(obj)

Create a new meta-object to inspect another object.

Kind: static method of reflection

Param Type Description
obj Object | function Object or function to inspect.

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);

obj.hasProperty(property, includePrototype) ⇒ Boolean

Check for a given property.

Kind: instance method of Obj
Returns: Boolean - Whether or not the object has the property.

Param Type Description
property string Property name to check.
includePrototype Boolean True (default) to look up the prototype chain as well, false to only look at direct object.

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.hasProperty('hello');			//true
reflectionObj.hasProperty('hello', false);	//false
reflectionObj.hasProperty('sayHello');		//false

obj.hasMethod(method, includePrototype) ⇒ Boolean

Check for a given method.

Kind: instance method of Obj
Returns: Boolean - Whether or the not the object has the method.

Param Type Description
method string Method name to check.
includePrototype Boolean True to look up the prototype chain as well, false to only look at direct object.

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.hasProperty('sayHello');	//true
reflectionObj.hasProperty('hello');		//false

obj.getName() ⇒ string

Get the name.

Kind: instance method of Obj
Returns: string - Object's name.
Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getName();	//'testObj'

obj.getConstructor() ⇒ function

Get the constructor.

Kind: instance method of Obj
Returns: function - Object's constructor.
Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);

obj.getConstructorParameters() ⇒ Array

Get the constructors parameters.

Kind: instance method of Obj
Returns: Array - Object constructor's parameter names.
Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getConstructor();	// [function]

obj.getMethodParameters(method) ⇒ Array

Get a methods parameters.

Kind: instance method of Obj
Returns: Array - Object method's parameter names.

Param Type Description
method string Method name.

Example

const reflection = require('js-reflection');

function testObj(text) { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getConstructorParameters();	// ['text']

obj.getMethods(includePrototype) ⇒ Array

Get all the methods.

Kind: instance method of Obj
Returns: Array - Object method's names.

Param Type Description
includePrototype Boolean True to look up the prototype chain as well, false to only look at direct object.

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getMethods();		//['sayHello']
reflectionObj.getMethods(false);	//[]

obj.getMethod(method, includePrototype) ⇒ function

Get a specific method.

Kind: instance method of Obj
Returns: function - The method.

Param Type Description
method string Method name.
includePrototype Boolean True to look up the prototype chain as well, false to only look at direct object.

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getMethod('sayHello');	//[function]

obj.getProperties(includePrototype) ⇒ Array

Get all the properties.

Kind: instance method of Obj
Returns: Array - Object properties's names.

Param Type Description
includePrototype Boolean True to look up the prototype chain as well, false to only look at direct object.

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getProperties();		//['hello']
reflectionObj.getProperties(false);	//[]

obj.getProperty(property, includePrototype) ⇒ Any

Get a specific property.

Kind: instance method of Obj
Returns: Any - The property.

Param Type Description
property string Property's name.
includePrototype Boolean True to look up the prototype chain as well, false to only look at direct object.

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getProperty('hello');	//'test'

obj.getPropertiesAndMethods(includePrototype) ⇒ Array

Get all the properties and methods.

Kind: instance method of Obj
Returns: Array - Object properties's and methods names.

Param Type Description
includePrototype Boolean True to look up the prototype chain as well, false to only look at direct object.

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getPropertiesAndMethods();		//['hello', 'sayHello']
reflectionObj.getPropertiesAndMethods(false);	//[]

obj.getPropertyOrMethod(propertyOrMethod, includePrototype) ⇒ Any

Get a specific property or method.

Kind: instance method of Obj
Returns: Any - The property or method.

Param Type Description
propertyOrMethod string Property or method's name.
includePrototype Boolean True to look up the prototype chain as well, false to only look at direct object.

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getPropertyOrMethod('hello');	//'test'
reflectionObj.getPropertyOrMethod('sayHello');	//'[function]'

reflection.Func(func)

Create a new meta-funct to inspect another object.

Kind: static method of reflection

Param Type Description
func function Function to inspect.

Example

const reflection = require('js-reflection');

function testFunc() { };

const reflectionFunc = new reflection.Func(testFunc);

func.getName() ⇒ string

Get the name.

Kind: instance method of Func
Returns: string - Functions's name.
Example

const reflection = require('js-reflection');

function testFunc() { };

const reflectionFunc = new reflection.Func(testFunc);
reflectionFunc.getName();	//'testFunc'

func.getParameters() ⇒ Array

Get the parameters.

Kind: instance method of Func
Returns: Array - Functions's parameter names.
Example

const reflection = require('js-reflection');

function testFunc(text) { };

const reflectionFunc = new reflection.Func(testFunc);
reflectionFunc.getParameters();	//['text']