node中exports和module.exports的關系及使用


    在node中,需要記住,在使用exports和module.exports的時候,實際輸出的是module.exports。

    exports指向module.exports,是module.exports的引用,所以,當使用 exports.a = x 的時候,通過引用關系,造成了module.exports.a = x。當使用 exports = x 的時候,造成了exports不再指向module.exports,所以,僅改變了exports,並沒有改變module.exports,也就並沒有對輸出起作用。

    當 exports.a = x 和 module.exports.a = xx 一起使用時,最后輸出出來的 a = xx;因為一般我們會把module.exports.a == xx放到后面來寫,實際上等效於 module.exports.a = x ,然后又執行了module.exports.a = xx,只是進行了一個重新賦值。

 同樣,當 exports.a = x 和 module.exports = xx一起使用時,最后的輸出只有 xx,我們再來進行一次轉化, exports.a = x 等效於module.exports.a = x, 等效於 module.exports = {a: x}, 然后又執行了module.exports = xx, 實際也是進行了一個重新賦值。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM