简体   繁体   中英

Use module.exports function within same file

I am trying to just create a simple constant that will be available throughout my application. I don't know if this is the best way to do it.

I have

module.exports = { GET: 'get', POST: 'post', DELETE: 'delete' }

if i wanted to reference this inside the same js file how would i go about doing it? currently i have

var http = module.exports = { GET: 'get' }

now i should be able to access it within the .js file as http.GET does this work? and what would be the difference between

var http = module.exports.GET = 'get' and var http = module.exports = { GET: 'get' } ? Are they exactly identical or is there some differences between the two.

does this work?

Yes.

But you could also drop the http and just refer to module.exports.GET every time.

what would be the difference between var http = module.exports.GET = 'get' and var http = module.exports = { GET: 'get' } ?

The most obvious difference is that in one case your http variable contains an object and in the other a string.

Also, in the first case you are extending the existing module.exports object (which is initialised as an empty object, so no difference in the result) and in the second case you are overwriting the .exports property of the module with a new object. This only makes a real difference if you're doing this multiple times though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM