2

Several times in javascript I've seen a constructor function take in one parameter. The constructor will initialize all the fields for the object to default values and then use the parameter to overwrite a select few properties. This way you can create a mostly default object save for a field or two with all the remaining properties being present on the object.

What would you name this argument? What is this pattern called?

For example, in Angular, here's a simplified example of how we create our models from our API responses:

angular.module('app')
  .factory('Person', function() {
    function Person(apiPayload) {
      function defaults() {
        return {
          id: null,
          name: null,
          age: null,
          address: null,
          phone: null,
          email: null
        };
      }

      angular.extend(this, defaults(), apiPayload);
    }

    return Person;
  });

It could be used like this:

var person = new Person({ name: 'Corey' });

And person would have all their fields present on the object filled with null except for the name field which would properly have 'Corey'. We name it apiPayload because the only thing we feed to this particular constructor is the data received from our API. I'm looking for a more general name.

closed as primarily opinion-based by gnat, user40980, JeffO, user22815, GlenH7 Jul 16 '15 at 18:18

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

3

configuration object or config object are the terms often used to describe this construct.

It allows to rearrange the parameters in any order, because the properties of the generic object have no ordering. And you don't need a long list of optional parameters.

  • 1
    a reference to where this term is used would be more convincing in your answer – Fuhrmanator Jul 16 '15 at 14:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.