javascript - Nodejs referencing module.exports -


i'm trying js code shared between browser , nodejs server. that, use these practises: http://caolanmcmahon.com/posts/writing_for_node_and_the_browser/

the problem when want export function, not object. in node like:

var constructor = function(){/*code*/}; module.exports = constructor; 

so when require used can do:

var constructor = require('module.js'); var oinstance = new constructor(); 

the problem when try reference module.exports object in module , use reference overwrite function. in module be:

var constructor = function(){/*code*/}; var reference = module.exports; reference = constructor; 

why doesn't work? don't want use easy solution insert if inside clean code, want understand why illegal, though reference===module.exports true.

thanks

it not work because reference not point module.exports, points object module.exports points to:

module.exports               \                  -> object               /       reference 

when assign new value reference, change reference points to, not module.exports points to:

module.exports               \                  -> object  reference -> function 

here simplified example:

var = 0; var b = a; 

now, if set b = 1, value of a still 0, since assigned new value b. has no influence on value of a.

i want understand why illegal, though reference===module.exports true

it not illegal, how javascript (and other languages) work. reference === module.exports true, because before assignment, both refer same object. after assignment, references refers different object modules.export.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -