๐3์ฅ(1) ํค์๋๐
REPL
js ํ์ผ ์คํํ๊ธฐ
๋ ธ๋ ๋ชจ๋; CommonJS ๋ชจ๋, ECMAScript ๋ชจ๋
๋ ธ๋ ๋ด์ฅ ๊ฐ์ฒด
js๋ ์คํฌ๋ฆฝํธ ์ธ์ด (์ปดํ์ผ ๋ถํ์)
Read Eval Print Loop
์
๋ ฅํ ์ฝ๋๋ฅผ ์ฝ๊ณ , ํด์ํ๊ณ , ๊ฒฐ๊ณผ๋ฌผ์ ๋ฐํํ๊ณ , ์ข
๋ฃํ ๋๊น์ง ๋ฐ๋ณตํ๋ค
ํฐ๋ฏธ๋์์ node ๋ช
๋ น ์
๋ ฅ ์
> ๋ชจ์์ ํ๋กฌํํธ
js ์ฝ๋ ์
๋ ฅ ๊ฐ๋ฅ
js ํ์ผ์ ๋ง๋ค์ด ์คํ
function helloWorld() {
console.log('Hello World');
helloNode();
}
function helloNode() {
console.log('Hello Node');
}
helloWorld();
node ๋ช ๋ น์ด๋ก ํ์ผ ์คํ(.js ํ์ฅ์ ์๋ต ๊ฐ๋ฅ)
๋ชจ๋: ํน์ ํ ๊ธฐ๋ฅ์ ํ๋ ํจ์๋ ๋ณ์๋ค์ ์งํฉ
// func.js
const { odd, even } = require('./var');
function checkOddOrEven(num) {
if (num % 2) { // ํ์์ด๋ฉด
return odd;
}
return even;
}
module.exports = checkOddOrEven;
// index.js
const { odd, even } = require('./var');
const checkNumber = require('./func');
function checkStringOddOrEven(str) {
if (str.length % 2) { // ๊ธ์์๊ฐ ํ์์ด๋ฉด
return odd;
}
return even;
}
console.log(checkNumber(10));
console.log(checkStringOddOrEven('hello'));
// var2.js
exports.odd = 'CommonJS ํ์์
๋๋ค';
exports.even = 'CommonJS ์ง์์
๋๋ค';
console.log('require๊ฐ ๊ฐ์ฅ ์์ ์ค์ง ์์๋ ๋ฉ๋๋ค.');
module.exports = '์ ๋ฅผ ์ฐพ์๋ณด์ธ์.';
require('./var');
console.log('require.cache์
๋๋ค.');
console.log(require.cache);
console.log('require.main์
๋๋ค.');
console.log(require.main === module);
console.log(require.main.filename);
// dep1.js
const dep2 = require('./dep2');
console.log('require dep2', dep2);
module.exports = () => {
console.log('dep2', dep2);
};
// dep2.js
const dep1 = require('./dep1');
console.log('require dep1', dep1);
module.exports = () => {
console.log('dep1', dep1);
};
//dep-run.js
const dep1 = require('./dep1');
const dep2 = require('./dep2');
dep1();
dep2();
// var.mjs
export const odd = 'MJS ํ์์
๋๋ค';
export const even = 'MJS ์ง์์
๋๋ค';
// func.mjs
import { odd, even } from './var.mjs';
function checkOddOrEven(num) {
if (num % 2) { // ํ์๋ฉด
return odd;
}
return even;
}
export default checkOddOrEven;
// index.mjs
import { odd, even } from './var.mjs';
import checkNumber from './func.mjs';
function checkStringOddOrEven(str) {
if (str.length % 2) { // ํ์๋ฉด
return odd;
}
return even;
}
console.log(checkNumber(10));
console.log(checkStringOddOrEven('hello'));
// dynamic.js
const a = false;
if (a) {
require('./func'); // ์คํ๋์ง ์์
}
console.log('์ฑ๊ณต');
// dynamic.mjs
const a = false;
if (a) {
import './func.mjs'; // ES ๋ชจ๋์ if๋ฌธ ์์์ import ๋ถ๊ฐ
}
console.log('์ฑ๊ณต');
// dynamic.mjs
const a = true;
if (a) {
const m1 = await import('./func.mjs');
console.log(m1);
const m2 = await import('./var.mjs');
console.log(m2);
}
์คํ ์ ํ์ฌ ํ์ผ๋ช , ํ์ฌ ํ์ผ ๊ฒฝ๋ก๋ก ๋ฐ๋
// filename.js
console.log(__filename);
console.log(__dirname);
// filename.mjs
console.log(import.meta.url);
console.log('__filename์ ์๋ฌ');
console.log(__filename);
// globalA.js
module.exports = () => global.message;
// globalB.js
const A = require('./globalA');
global.message = '์๋
ํ์ธ์';
console.log(A());
globalB์์ ๋ฃ์ global.message ๊ฐ์ globalA์์๋ ์ ๊ทผ ๊ฐ๋ฅ
const string = 'abc';
const number = 1;
const boolean = true;
const obj = {
outside: {
inside: {
key: 'value',
},
},
};
console.time('์ ์ฒด์๊ฐ');
console.log('ํ๋ฒํ ๋ก๊ทธ์
๋๋ค ์ผํ๋ก ๊ตฌ๋ถํด ์ฌ๋ฌ ๊ฐ์ ์ฐ์ ์ ์์ต๋๋ค');
console.log(string, number, boolean);
console.error('์๋ฌ ๋ฉ์์ง๋ console.error์ ๋ด์์ฃผ์ธ์');
console.table([{ name: '์ ๋ก', birth: 1994 }, { name: 'hero', birth: 1988}]);
console.dir(obj, { colors: false, depth: 2 });
console.dir(obj, { colors: true, depth: 1 });
console.time('์๊ฐ์ธก์ ');
for (let i = 0; i < 100000; i++) {}
console.timeEnd('์๊ฐ์ธก์ ');
function b() {
console.trace('์๋ฌ ์์น ์ถ์ ');
}
function a() {
b();
}
a();
console.timeEnd('์ ์ฒด์๊ฐ');
// timer.js
const timeout = setTimeout(() => {
console.log('1.5์ด ํ ์คํ');
}, 1500);
const interval = setInterval(() => {
console.log('1์ด๋ง๋ค ์คํ');
}, 1000);
const timeout2 = setTimeout(() => {
console.log('์คํ๋์ง ์์ต๋๋ค');
}, 3000);
setTimeout(() => {
clearTimeout(timeout2);
clearInterval(interval);
}, 2500);
const immediate = setImmediate(() => {
console.log('์ฆ์ ์คํ');
});
const immediate2 = setImmediate(() => {
console.log('์คํ๋์ง ์์ต๋๋ค');
});
clearImmediate(immediate2);
// timerPromise.js
import { setTimeout, setInterval } from 'timers/promises';
await setTimeout(3000);
console.log('3์ด๋ค ์คํ');
for await (const startTime of setInterval(1000, Date.now())) {
console.log('1์ด๋ง๋ค ์คํ', new Date(startTime));
}
process ๊ฐ์ฒด: ํ์ฌ ์คํ๋๊ณ ์๋ ๋
ธ๋ ํ๋ก์ธ์ค์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์
REPL์ ์
๋ ฅํด ํ์ธ ๊ฐ๋ฅ
ํ๊ฒฝ ๋ณ์ ํ์ธ ๋ช
๋ น
process.env์ ํ๊ฒฝ ๋ณ์๋ฅผ ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ์ 6.2์ dotenv ์ฌ์ฉ๋ฒ ์ฐธ์กฐ
// nextTick.js
setImmediate(() => {
console.log('immediate');
});
process.nextTick(() => {
console.log('nextTick');
});
setTimeout(() => {
console.log('timeout');
}, 0);
Promise.resolve().then(() => console.log('promise'));
์คํ ์ค์ธ ๋ ธ๋ ํ๋ก์ธ์ค ์ข ๋ฃ
๋น์นธ ๋ฌธ์ (๋น์นธ ๋๋๊ทธ ์ ์ ๋ต ํ์)
์ฝ๋ ๋ฌธ์
1. ๋ค์ CommonJS ๋ชจ๋์ EMCAScript ๋ชจ๋ ํ์์ผ๋ก ๋ณ๊ฒฝํ์ธ์.
// var.js
const odd = 'CommonJS ํ์์
๋๋ค';
const even = 'CommonJS ์ง์์
๋๋ค';
module.exports = {
odd,
even,
};
// func.js
const { odd, even } = require('./var');
function checkOddOrEven(num) {
if (num % 2) { // ํ์์ด๋ฉด
return odd;
}
return even;
}
module.exports = checkOddOrEven;
// index.js
const { odd, even } = require('./var');
const checkNumber = require('./func');
function checkStringOddOrEven(str) {
if (str.length % 2) { // ๊ธ์์๊ฐ ํ์์ด๋ฉด
return odd;
}
return even;
}
console.log(checkNumber(10));
console.log(checkStringOddOrEven('hello'));
// var.mjs
export const odd = 'MJS ํ์์
๋๋ค';
export const even = 'MJS ์ง์์
๋๋ค';
// func.mjs
import { odd, even } from './var.mjs';
function checkOddOrEven(num) {
if (num % 2) { // ํ์๋ฉด
return odd;
}
return even;
}
export default checkOddOrEven;
// index.mjs
import { odd, even } from './var.mjs';
import checkNumber from './func.mjs';
function checkStringOddOrEven(str) {
if (str.length % 2) { // ํ์๋ฉด
return odd;
}
return even;
}
console.log(checkNumber(10));
console.log(checkStringOddOrEven('hello'));
2. ๋ค์ ์ฝ๋์ ์ถ๋ ฅ์ ์์ํ์ธ์.
// timer.js
const timeout = setTimeout(() => {
console.log('1.5์ด ํ ์คํ');
}, 1500);
const interval = setInterval(() => {
console.log('1์ด๋ง๋ค ์คํ');
}, 1000);
const timeout2 = setTimeout(() => {
console.log('์คํ๋์ง ์์ต๋๋ค');
}, 3000);
setTimeout(() => {
clearTimeout(timeout2);
clearInterval(interval);
}, 2500);
const immediate = setImmediate(() => {
console.log('์ฆ์ ์คํ');
});
const immediate2 = setImmediate(() => {
console.log('์คํ๋์ง ์์ต๋๋ค');
});
clearImmediate(immediate2);
์ฆ์ ์คํ
1์ด๋ง๋ค ์คํ
1.5์ด ํ ์คํ
1์ด๋ง๋ค ์คํ
์ถ์ฒ: ์กฐํ์, ใNode.js ๊ต๊ณผ์ใ ๊ฐ์ ํ 3ํ, ๊ธธ๋ฒ(2022), 3-1~3-4์ฅ.
[๋ ธ๋ 2] 6์ฅ. ์ต์คํ๋ ์ค ์น ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2023.11.10 |
---|---|
[๋ ธ๋ 2] 5์ฅ. ํจํค์ง ๋งค๋์ (0) | 2023.11.03 |
[๋ ธ๋ 2] 4์ฅ. http ๋ชจ๋๋ก ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2023.11.03 |
[๋ ธ๋ 2] 3์ฅ. ๋ ธ๋ ๊ธฐ๋ฅ ์์๋ณด๊ธฐ (2) (0) | 2023.10.13 |
[๋ ธ๋ 2] 2์ฅ. ์์๋ฌ์ผ ํ ์๋ฐ์คํฌ๋ฆฝํธ (0) | 2023.09.29 |