-1

using lodash assign method, can I do

_.assign(self, {
            inputContext: createAddonInputContext(),
            currentEmployee: _.first(self.inputContext.employeeList),
        });

the currentEmployee property value calculation depends on inputContext being assigned already. My question is, will this always work, or it can break in some environments i.e. when currentEmployee will be assigned first? In other words, does the order in which properties appear in source object literal guarantee the same order when the value are being evaluated? I'm aware that when we do for...in iteration over javascript object, the order isn not certain. Hence my question.

dKab
  • 2,658
  • 5
  • 20
  • 35
  • 1
    In fact, this will *never* work. The object literal and its properties are evaluated even before `assign` is called. – Bergi Aug 23 '16 at 11:13

1 Answers1

2

the currentEmploye property value calculation depends on inputContext being assigned already.

Then you will need to assign them separately:

self.inputContext = createAddonInputContext();
self.currentEmployee = _.first(self.inputContext.employeeList);

will this always work, or it can break in some environments i.e. when currentEmployee will be assigned first?

This never works, it will break in all environments. The object literal and its two property values are evaluated before the call to assign is executed and anything is assigned to self.

Does the order in which properties appear in source object literal guarantee the same order when the value are being evaluated?

Yes, in fact object property values are always evaluated in source order, but this doesn't matter here, the values are inaccessible as properties before the whole object is created.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375