node.js에서 exports와module.exports 사이의 애증과 원한
4836 단어 node.jsmodule.exportsexports애증
우리들의 전생
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
가령, 우리는 현재 두 개의 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 ::: {}
결론
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Express + AWS S3 이미지 업로드하기웹 사이트 및 모바일 애플리케이션 등에서 원하는 양의 데이터를 저장하고 보호할 수 있다. 데이터에 대한 액세스를 최적화, 구조화 및 구성할 수 있는 관리 기능을 제공한다. AWS S3 에 저장된 객체에 대한 컨테이너...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.