node.js에서 exports와module.exports 사이의 애증과 원한

우리들의 전생

  • 무엇입니까
  • 그래, 네가 생각하는 대로, 우리는 함수를 만드는 대상이야. 리퀘어는 우리가 필요해.말하자면 아직 대상이다.
    Here is an eye-opener - module.exports is the real deal. exports is just module.exports’s little helper. Your module returns module.exports to the caller ultimately, not exports. All exports does is collect properties and attach them to module.exports IF module.exports doesn’t have something on it already. If there’s something attached to module.exports already, everything on exports is ignored.ヾ(≥O≤)〃오오~(영어군은 이미 길에 있으니 객관적으로 서두르지 마라)
    이 위에서 도대체 무슨 말을 하고 있는 거야, 사실은 한마디로 "이제는 시야를 넓힐 때야"- come on
  • module.exports는 진실하게 존재하는 대상이다
  • exports는 모듈일 뿐입니다.exports의 조수
  • 최종적으로 호출자(require)에게module.exports가 아니라 exports
  • exports는 조수로서 도대체 무엇을 하는 걸까.그것은 주로module를 돕는다.exports에서 속성을 수집하여 부여하기;하지만 전제는module이다.exports 자체 부여되지 않음
  • 반대로module.exports 자체는 이미 부여된 값이고, 게다가 exports의 속성 값은 모두 무시됩니다
  • 뭘 할 수 있겠어
  • 글쎄, 대상으로서 당연히 전가해야지
  • 어떻게 사용하나요
  • 다음은 흥미진진하게 이야기할 것이다.
    가령, 우리는 현재 두 개의 js 파일 리더가 있다고 가정한다.js와employee.js, 지도자 호출 직원

    사랑과 정


    1.module.exports: 넌 내 거야.
    exports는 하나의 형삼에 해당하며module를 가리킨다.exports
    employee.js
    /**
     * @autor : Eric.zhu
     * @email : [email protected]
     * @date  : 2015-05-21 23:00:10
     */
    
    "use strict"
    
    function coding(){
        console.log('writing code is a great project...');
    }
    
    //.. coding , coding() 
    
    //.. module.exports.coding = coding 
    exports.coding = coding;
    
    console.log('i am exports:::',exports);
    console.log('i am module.exports:::',module.exports);
    
    console.log('exports == module.exports   ====>  ',exports == module.exports);

    console:
    i am exports::: { coding: [Function: coding] }
    i am module.exports::: { coding: [Function: coding] }
    exports == module.exports   ====>   true

    leader.js 안의 것은 군말하지 않겠습니다. 여기는 이미 문제를 충분히 설명했습니다. 누구의 값을 가져가느냐가 중요합니까?
     2.module.exports: 내 건 내 거야. 물론 니 거야.
    1의 코드 주석을 참고하십시오. 아직 이해하지 못한다고 말하지 마십시오. (만약 불행하게도 총을 누우면 자동으로 벽을 향해 O () O ~)

    한과 수


    3.module.exports:내가 있어도 네가 없어(횡포)
    employee.js
    /**
     * @autor : Eric.zhu
     * @email : [email protected]
     * @date  : 2015-05-21 23:00:10
     */
    
    "use strict"
    
    function coding(){
        console.log('writing code is a great project...');
    }
    
    function rest(){
        console.log('i am going to die,wo must have a rest!!!');
    }
    
    module.exports = coding;
    
    exports.rest = rest;
    
    console.log('i am exports:::',exports);
    console.log('i am module.exports:::',module.exports);
    
    console.log('exports == module.exports   ====>  ',exports == module.exports);

    console:
    i am exports::: { rest: [Function: rest] }
    i am module.exports::: function coding(){
        console.log('writing code is a great project...');
    }
    exports == module.exports   ====>   false

    이미 사이가 틀어졌어. 그럼, 우리 다음에 보자, 리더.js에서 Require가 얻은 건 도대체 누구일까요?
    leader.js
    /**
     * @autor : Eric.zhu
     * @email : [email protected]
     * @date  : 2015-05-21 23:00:10
     */
    
    "use strict"
    
    var coding = require('./employee');
    
    console.log('who am i ::: ',coding);
    
    coding();

    console:
    who am i :::  function coding(){
        console.log('writing code is a great project...');
    }
    writing code is a great project...

    분명히exports가 부여되었습니다. 그러면 exports의 모든 속성은 무시됩니다. 아하하
    4.module.exports: 나도 너도 없어.
    employee.js
    /**
     * @autor : Eric.zhu
     * @email : [email protected]
     * @date  : 2015-05-21 23:00:10
     */
    
    "use strict"
    
    function coding(){
        console.log('writing code is a great project...');
    }
    
    function rest(){
        console.log('i am going to die,wo must have a rest!!!');
    }
    
    //.. exports , 
    exports = rest;
    
    console.log('i am exports:::',exports);
    console.log('i am module.exports:::',module.exports);
    
    console.log('exports == module.exports   ====>  ',exports == module.exports);

    console:
    i am exports::: function rest(){
        console.log('i am going to die,wo must have a rest!!!');
    }
    i am module.exports::: {}
    exports == module.exports   ====>   false

    leader.js의 console:
    who am i :::  {}

    결론

  • exports는 모듈입니다.exports의 인용, 양자가 각각 속성에 값을 부여할 때 동등한 효과를 얻을 수 있으니 마음에 드는 것을 선택하세요
  • module.exports가 부여된 후 모든 exports 속성 부여는 무시됩니다
  • require가 최종적으로 얻은 것은 모두module이다.exports의 값이 아니라 exports
  • 모듈이 특수한 대상 유형이 되고 싶을 때(eg:boolean,number,date,JSON,string,function,array 및 기타) 모듈을 사용하는 것이 좋습니다.exports
  • 당신의 모듈이 일반적인 모듈의 실례가 되기를 원한다면 exports로 마음대로 하세요
  • 시간적 촉박으로 인해 이해하기 어렵거나 이해하기 어려운 부분이 있을 수 있으니 언제든지 바로잡아 주십시오.

    좋은 웹페이지 즐겨찾기