Decorators

It's just a pattern 

function checkPermissionDecorator(f) {
  return function() {
    if (user.isAdmin()) {
        f();
    } else {
        alert('Not an admin yet');
    }
  };
}

save = checkPermissionDecorator(save);

Аспектно-ориентированное программирование

function doublingDecorator(f) {
  return function() {
    return 2*f.apply(this, arguments)
  } 
}

function sum(a, b) {
  return a + b
};

var doubleSum = doublingDecorator(sum);

alert(doubleSum(1,2));

DefineProperty

DefineProperties

const literal = {};
const dog = Object.defineProperties(literal, {
  name: {
    value: 'Doug',
    writable: true,
    enumerable: true,
    configurable: true
  },
  legs: {
    value: 4,
    writable: true,
    enumerable: true,
    configurable: true
  }
});

Proxy

defineProperty + Proxy

function readonly (target, key, descriptor) {
  descriptor.writable = false;
  return descriptor;
}
function readonly (key) {
	return function (target, key, descriptor) {
		descriptor.writable = false;
		return descriptor;
	}	
}

TS

https://github.com/jayphelps/core-decorators.js

Where is decorators?

https://github.com/hemanth/es-next

Decorators

By Vladimir

Decorators

  • 94