Reliable and Recognized JavaScript Certification Online #

Metaprogramming

Metaprogramming is a technique which allows programs to treat their own code or code of other programs as data. Using metaprogramming programs can read, analyse, generate and modify other programs and itself while running. This gives programs bigger flexibility.

JavaScript introduced Proxy object type and Reflection object which allow you to program at meta level of the language.

 

Proxy

Proxy allows you to intercept defaults and implement custom behaviour for certain operations performed on objects. For example, these operations include access to properties, assignment, method invocation, enumeration, etc.

There are three terms related to proxy:

target – it is the object that proxy visualises.
traps – the methods that provide property access.
handler – the placeholder object that contains the traps.

The syntax to create a proxy is as follows:

var proxy = new Proxy(target, handler);

The target is an object to wrap with Proxy.
The handler is an object whose methods define the behaviour of the proxy when an operation is performed on it.

Example:

var colorCode = {
    red: '#ff0000',
    blue: '#0000ff'
};

var colorHandler = {
    get: function(obj, prop) {
        var color = '#000000';
        if (prop in obj) {
            color = obj[prop];
        }
        return color;
    }
};

var colorProxy = new Proxy(colorCode, colorHandler);

colorProxy.green = '#008000';

console.log(colorProxy.red); // #ff0000
console.log(colorProxy.green); // #008000
console.log(colorProxy.navy); // #000000
Methods

apply() a trap for a function call.
construct() a trap for the new operator.
has() a trap for the in operator.
get() a trap for getting property values.
set() a trap for setting property values.
deleteProperty() a trap for the delete operator.
ownKeys() a trap for Object.getOwnPropertyNames.
getPrototypeOf() a trap for Object.getPrototypeOf.
setPrototypeOf() a trap for Object.setPrototypeOf.
isExtensible() a trap for Object.isExtensible.
preventExtensions() a trap for Object.preventExtensions.
getOwnPropertyDescriptor() a trap for Object.getOwnPropertyDescriptor.
defineProperty() a trap for Object.defineProperty.

 

Reflection

Reflection allows to examine the program structure and modify its own behavior at runtime.

Reflect is a built-in JavaScript object which provides methods for interceptable operations.

It has the same methods as Proxy object type.

Examples:

var result = Reflect.apply(Math.pow, undefined, [3, 2]); // 9

var obj = {x: 1};
var test1 = Reflect.has(obj, 'x'); // true 
var test2 = Reflect.has(obj, 'y'); // false

var bucket = {}; 
Reflect.set(bucket, 'capacity', 10); 
console.log(bucket.capacity); // 10

function multiply(x, y) {
  this.product = x * y;
}
var args = [6, 7];
var calc = Reflect.construct(multiply, args);
console.log(calc.product); // 42

var person = {
  name: 'Jack',
  age: 38
};
Reflect.deleteProperty(person, 'age');
console.log(person.name, person.age); // Jack undefined

var container = {a: 1, b: 20};
console.log(Reflect.get(container, 'b')); // 20

No Comments

Reply