Calling lodash _.defaults() before Object.defineProperty() In reviewing an older project of mine, I noticed that in a particular class definition I'm doing this:

var defaultOptions = { ... object of default options ... }

var SDKClient = function(options) {
    var self = this

    ...

    _.defaults(self, options, defaultOptions)

    Object.defineProperty(self, 'clientId', {
        enumerable: false
    })

    Object.defineProperty(self, 'clientSecret', {
        enumerable: false
    })
}


If I initialize the client object like this:

var client = new SDKClient({clientId: "x", clientSecret: "y"})


Should I expect there to be problems calling defineProperty after the property has already been merged in with _.defaults()? I don't see any errors or problems thrown when running tests, but this is a large project, and I can't be certain there isn't something obscuring errors. If it's not OK, why is it not OK?
I don't believe so _.defaults. The properties will be set on this but be enumerable. The call to Object.defineProperty  will just set those properties to not enumerable. If the property didn't exist then the value would be set as undefined and default descriptors.

Let's break out only the necessary.

Empty object.


var x = {};
console.log(Object.getOwnPropertyDescriptor(x, 'clientId'));




Object with clientId set.


var x = {
  clientId: 1
};
console.log(Object.getOwnPropertyDescriptor(x, 'clientId'));




Empty object.


var x = Object.defineProperty({}, 'clientId', {
  enumerable: false
});
console.log(Object.getOwnPropertyDescriptor(x, 'clientId'));



Note that the default descriptors are set.

Object with clientId set.


var x = Object.defineProperty({
  clientId: 1
}, 'clientId', {
  enumerable: false
});
console.log(Object.getOwnPropertyDescriptor(x, 'clientId'));




So if anything were to give you a problem then it would be this situation.



var x = Object.defineProperty({}, 'clientId', {
  enumerable: false
});
console.log(Object.getOwnPropertyDescriptor(x, 'clientId'));
x.clientId = 1;
console.log(Object.getOwnPropertyDescriptor(x, 'clientId'));