This is a tough one to word, but how can you have Model.method() and Model() be valid at the same time? The particular library that makes me ask this is mongoose (http://mongoosejs.com/docs/) where Model as an object represents a mongo collection and has several methods and Model as a function is a constructor for a mongo document with some methods. I'm trying to do something similar, but it just returns a function making typeof Model === 'function' and never object. It is as follows:
let model = (function(){
for(var i in queries){
if(typeof i == 'function'){
if(i == 'insert'){
continue;
}
this[i] = function(){
queries[i].apply(this, arguments); // queries is a separate module I've written that has methods for querying a DB
};
}
}
return (function(){
for(var i in arguments[0]){
if(!(i in schema) && typeof arguments[0][i] != typeof schema[i]){
this[i] = arguments[0][i];
}
else{
throw new Error('Invalid argument, key: ' + i + ' value: ' + arguments[0][i]);
}
}
for(var i in schema.methods){
this[i] = schema.methods[i];
}
});
})();
model. The fact it's an object isn't really being used (other than that we can have a reference to it) in the quoted code.