0

This is the code (specifically, 398 to 407):

https://github.com/openexchangerates/accounting.js/blob/master/accounting.js#L403

I don't understand this method:

// Use accounting.noConflict to restore `accounting` back to its original value.
// Returns a reference to the library's `accounting` object;
// e.g. `var numbers = accounting.noConflict();`
lib.noConflict = (function(oldAccounting) {
            return function() {
                // Reset the value of the root's `accounting` variable:
                root.accounting = oldAccounting;
                // Delete the noConflict method:
                lib.noConflict = undefined;
                // Return reference to the library to re-assign it:
                return lib;
            };
        })(root.accounting);

If I do var numbers = accounting.noConflict(), it puts the library in the numbers variable. What I don't understand is, why if I do numbers.noConflict it is undefined. I know lib.noConflict = undefined sets it to undefined, but isn't it then set to lib since the following code is return lib?

SRCP
  • 224
  • 2
  • 10
  • The `noConflict` function is clearly defined to be called only once and manages the `accounting` property of the library. By setting `lib.noConflict = undefined`, you will not be able to access the method from numbers – Jonathan Hamel May 06 '19 at 20:09
  • But isn't lib.noConflict always set to lib since that's what it returns? – SRCP May 06 '19 at 20:43
  • 1
    As you can see in the snippet, it's a `curried` function (nested function with context binding) which calls itself to set the initial binding. When calling `accounting.noConflict()`, execution will unset the `noConflict` method so it cannot be called a second time using the returned `lib`. Try debugging nested functions or `curried` functions to understand the behavior a little better. – Jonathan Hamel May 07 '19 at 01:50

0 Answers0