DCL

An elegant OOP with mixins + AOP for JavaScript.

Replacer

Replacer is a simple base class, which is very similar to Mixer. The difference is that it is used to override only existing properties, it doesn’t add new properties to instance.

It can be included with following commands:

Include Replacer
1
2
3
4
5
6
7
8
9
10
11
12
13
// node.js
var Replacer = require("dcl/bases/Replacer");
...

// AMD (code)
require(["dcl/bases/Replacer"], function(Replacer){
  ...
});

// AMD (definition)
define(["dcl/bases/Replacer"], function(Replacer){
  ...
});

Description

Here is its definition:

Replacer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var Replacer = dcl(null, {
  declaredClass: "dcl/bases/Replacer",
  constructor: function(x){
    var empty = {};
    for(var name in x){
      if(name in this){
        var t = x[name], e = empty[name];
        if(t !== e){
          this[name] = t;
        }
      }
    }
  }
});

As you can see it overrides existing properties with non-default values. All other properties are ignored. It is a “tame” version of Mixer, which can be used to keep instances clean of unwanted properties. Replacer can be used as a base, or as a mixin, but usually it serves as the most deepest base to take care of parameters.

Refer to Best practices for constructors for discussion on constructor’ signatures.

Examples

Mixer example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var Person = dcl(Replacer, {
  declaredClass: "Person",
  firstName:  "",
  middleName: "",
  lastName:   ""
  constructor: function(){
    // no need to deal with parameters - they are already copied
    // on this instance by Mixer
    console.log("Hello " + this.firstName);
  },
  getFullName: function(){
    var name = [];
    if(this.firstName){
      name.push(this.firstName);
    }
    if(this.middleName){
      name.push(this.middleName);
    }
    if(this.lastName){
      name.push(this.lastName);
    }
    return name.join(" ");
  }
});

var bob = new Person({
  firstName:   "Robert",
  lastName:    "Smith",
  dateOfBirth: new Date(1985, 11, 31),
  shortName:   "Bob",
  getFullName: function(){
    return "Robert 'Bob' Smith";
  }
});

console.log(bob.getFullName()); // Robert 'Bob' Smith
console.log(bob.firstName);     // Robert
console.log(bob.shortName);     // unknown

Fork me on GitHub