0

I have a JavaScript object with some static attribute values, dynamic attribute values and methods. Each time I need one of these objects, I will need 10 of them. Each of the 10 objects gets initialized by a dedicated object literal. That happens under 3 different contexts of a user doing something on a data entry form. User actions can cause the contexts to happen in any order, any number of times, but the same 10 objects will always be created in each context. By "same" I mean the static values for a "no_matl" object will be identical each time a "no_matl" object is created ... only a few dynamic attribute values (field value, previous value, date/time, context ID) are different for each context.

Is there a smarter way to do the initialization currently done with the const object literal? Originally I passed a bunch of params to the constructor and initialized the static attributes from those. The object literal approach seemed cleaner. Maybe there's a better way?

// object literals used to initialize a each of the 10 
// different type objects.
const FIELD_NOMATERIAL = {
  DispName: 'No Material',
  DbName: 'NO_MATERIAL',
  TrueVal: 'Yes',
  InitVal: '',
  DispWhenSet: 'yes',
  DispWhenNotSet: ''
};

const FIELD_CPCAT = { ... same attributes, different values ...};

const FIELD_HCN = { ... same attributes, different values ...};
// ... 7 more like this ...

// context 1
var no_matl = new MyField(FIELD_NOMATERIAL),
    cpcap = new MyField(FIELD_CPCAT),
    hcn = new MyField(FIELD_HCN)  .... 7 more like this

// object definition
function MyField() {
  if (arguments.length == 1 && typeof(arguments[0]) === 'object' ) {
      this.DispName = arguments[0].DispName ;
      this.DbName = arguments[0].DbName ;
      // .... etc for rest of static attributes ...
  }
}
3
  • you cannot have "same attributes, different values" with object spread or object literal. you will end up with same keys and same values Commented Oct 29, 2019 at 4:42
  • just use object spread to "compose" the object you want const foo = { ...blah, fizz: 'buzz' } Commented Oct 29, 2019 at 4:43
  • I've never heard of an "object spread". I need to do some catching-up. Commented Nov 9, 2019 at 0:11

1 Answer 1

1

Sounds like what you want is a copy of the original object that can change values without changing the original. Try this:

const FIELD_NOMATERIAL = {
  DispName: 'No Material',
  DbName: 'NO_MATERIAL',
  TrueVal: 'Yes',
  InitVal: '',
  DispWhenSet: 'yes',
  DispWhenNotSet: ''
};

function getFreshCopy(original) {
  return Object.assign({}, original);
}

var no_matl = getFreshCopy(FIELD_NOMATERIAL);

Using Object.assign({}, obj) will create a new copy that can be changed without the original values changing. no_matl can be adjusted and FIELD_NOMATERIAL remains in its original state.

Note that const means the variable cannot be assigned a new value. It does not mean that the contents of the object cannot be changed. That means the following is true:

const noChange = { a: 7 };
noChange.a = 8;      // this is fine because 'a' is allowed to change
noChange = "hello";  // this gives TypeError: Assignment to constant variable.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.