코드로 설명하는 것이 가장 쉬운 방법 : 예상대로노드 모듈 - 변수를 반출하는 것과 반출하는 함수를 반출하는 모듈?
##### module.js
var count, incCount, setCount, showCount;
count = 0;
showCount = function() {
return console.log(count);
};
incCount = function() {
return count++;
};
setCount = function(c) {
return count = c;
};
exports.showCount = showCount;
exports.incCount = incCount;
exports.setCount = setCount;
exports.count = count; // let's also export the count variable itself
#### test.js
var m;
m = require("./module.js");
m.setCount(10);
m.showCount(); // outputs 10
m.incCount();
m.showCount(); // outputs 11
console.log(m.count); // outputs 0
보낸 기능은 작동합니다. m.count도 11
모든 것이 가치에 지나지 않습니다! 함수와 객체는 항상 참조로 전달됩니다. – zetlen
@zetlen 아니요 값으로 전달됩니다. 그들이 전달하는 값은 객체에 대한 참조입니다. 참조에 의한 전달은 포인터입니다. 포인터가 없습니다 – Raynos