Book Image

Mastering Web Application Development with Express

By : Alexandru Vladutu
Book Image

Mastering Web Application Development with Express

By: Alexandru Vladutu

Overview of this book

Table of Contents (18 chapters)
Mastering Web Application Development with Express
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Extending objects in a reusable way


We have probably been confronted with situations where we have to assign multiple properties to the same object, as shown in the following example:

person.name = values[0];
person.age = values[1];
person.job = values[2];

In the preceding example, there are only three properties, but when we have more, it becomes kind of tedious to write object.property = value each time. There is a better way to do this: by using the xtend module (https://www.npmjs.org/package/xtend):

var xtend = require('xtend');
person = xtend(person, {
  name: values[0],
  age: values[1],
  job: values[2]
});

The preceding code looks much cleaner, doesn't it?

These properties from an object (the source) are merged into another object (the target). This approach enables us to use mixins efficiently and in an elegant manner.

Note

Mixins allow objects to reuse existing properties or functions (from other objects) without having to redefine them. This pattern facilitates the inheritance of a...