DCL

An elegant OOP with mixins + AOP for JavaScript.

dcl.prop() and dcl.Prop

Version 2.x

dcl.Prop

dcl.Prop is a constructor function used to define a property descriptor decorator. In most cases, it is not constructed manually, but created by dcl.prop() described below.

The constructor creates a super-simple object with one property: x, which is an argument of the constructor. This constructor is needed solely for possible introspections with instanceof.

dcl.prop()

dcl.prop(arg) is a function that takes the property descriptor (described in Object.defineProperty()), and returns an instance of dcl.Prop.

This is its full definition:

Implementation of dcl.prop()
1
2
3
dcl.prop = function (arg) {
  return dcl.Prop(arg);
};

It is used by dcl to mark explicitly property descriptors.

Examples

Define a property using a descriptor

Define a property
1
2
3
4
5
6
7
8
9
10
11
var A = dcl({
    x: dcl.prop({
        get: function ()  { return this.y || 0; },
        set: function (x) { return this.y = 2 * x; }
      })
  });

var a = new A();
console.log(a.x); // 0
a.x = 3;
console.log(a.x); // 6

Define a “class” using descriptors

All properties can be described as an object that contains property descriptors.

Use property descriptors
1
2
3
4
5
6
7
var A = dcl(dcl.prop({
    life: {value: 42},
    answer: {
      value: function () { return this.life; },
      enumerable: false
    }
  }));