1.0.7 • Published 7 years ago

js-reflection v1.0.7

Weekly downloads
9
License
GPL-3.0
Repository
github
Last release
7 years ago

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'.

ParamTypeDescription
objObjectObject 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

ParamTypeDescription
objObject | functionObject 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.

ParamTypeDescription
propertystringProperty name to check.
includePrototypeBooleanTrue (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.

ParamTypeDescription
methodstringMethod name to check.
includePrototypeBooleanTrue 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.

ParamTypeDescription
methodstringMethod 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.

ParamTypeDescription
includePrototypeBooleanTrue 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.

ParamTypeDescription
methodstringMethod name.
includePrototypeBooleanTrue 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.

ParamTypeDescription
includePrototypeBooleanTrue 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.

ParamTypeDescription
propertystringProperty's name.
includePrototypeBooleanTrue 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.

ParamTypeDescription
includePrototypeBooleanTrue 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.

ParamTypeDescription
propertyOrMethodstringProperty or method's name.
includePrototypeBooleanTrue 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

ParamTypeDescription
funcfunctionFunction 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']
1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago