node.js - How can a javascript function also be an object? -
this tough 1 word, how can have model.method() , model() valid @ same time? particular library makes me ask mongoose (http://mongoosejs.com/docs/) model object represents mongo collection , has several methods , model function constructor mongo document methods. i'm trying similar, returns function making typeof model === 'function'
, never object. follows:
let model = (function(){ for(var in queries){ if(typeof == 'function'){ if(i == 'insert'){ continue; } this[i] = function(){ queries[i].apply(this, arguments); // queries separate module i've written has methods querying db }; } } return (function(){ for(var 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: ' + + ' value: ' + arguments[0][i]); } } for(var in schema.methods){ this[i] = schema.methods[i]; } }); })();
all javascript functions objects. that's how javascript works.
function alert(message) { document.body.appendchild(document.createtextnode(message)); document.body.appendchild(document.createelement('br')); } function myfunction() { alert("this myfunction"); } myfunction.property = "hello"; myfunction.recursive = myfunction; myfunction(); alert(myfunction.property); alert(myfunction.name); myfunction.recursive.recursive.recursive.recursive.recursive();
Comments
Post a Comment