Search

util

util

util

: Node.jsμ—μ„œ λ‹€μ–‘ν•œ μœ ν‹Έλ¦¬ν‹° ν•¨μˆ˜λ“€μ„ μ œκ³΅ν•˜λŠ” λ‚΄μž₯ λͺ¨λ“ˆ
Promise νŒ¨ν„΄ λ³€ν™˜, 상속, μ„œμ‹ λ¬Έμžμ—΄ 생성, 객체 디버깅 λ“±μ˜ κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€.

μ •μ˜

util λͺ¨λ“ˆμ€ 각쒅 μœ ν‹Έλ¦¬ν‹° ν•¨μˆ˜λ“€μ„ μ œκ³΅ν•˜λŠ” Node.js의 λ‚΄μž₯ λͺ¨λ“ˆμž…λ‹ˆλ‹€.

문법

const util = require('util');
JavaScript
볡사

μ£Όμš” 속성 및 λ©”μ†Œλ“œ

λ©”μ†Œλ“œ λ˜λŠ” 속성
μ„€λͺ…
util.promisify
콜백 νŒ¨ν„΄μ˜ ν•¨μˆ˜λ₯Ό Promise νŒ¨ν„΄μœΌλ‘œ λ³€ν™˜ν•˜λŠ” λ©”μ†Œλ“œ
util.inherits
클래슀 κ°„μ˜ 상속을 κ΅¬ν˜„ν•˜λŠ” λ©”μ†Œλ“œ
util.format
μ„œμ‹ λ¬Έμžμ—΄μ„ μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ μƒμ„±ν•˜λŠ” λ©”μ†Œλ“œ
util.inspect
객체λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜μ—¬ 디버깅에 μ‚¬μš©ν•˜λŠ” λ©”μ†Œλ“œ

promisify()

util.promisify(original)
: 콜백 νŒ¨ν„΄μ˜ ν•¨μˆ˜λ₯Ό Promise νŒ¨ν„΄μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.
const fs = require('fs'); const readFile = util.promisify(fs.readFile);
JavaScript
볡사

inherits()

util.inherits(constructor, superConstructor)
: 클래슀 상속을 κ΅¬ν˜„ν•©λ‹ˆλ‹€.
function Base() { this.name = 'Base'; } function Child() { Base.call(this); this.name = 'Child'; } util.inherits(Child, Base);
JavaScript
볡사

format()

util.format(format[, ...args])
: μ„œμ‹ λ¬Έμžμ—΄μ„ μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ λ§Œλ“­λ‹ˆλ‹€.
const message = util.format('%s %s', 'Hello', 'World');
JavaScript
볡사

inspect()

util.inspect(object[, options])
: 객체λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜μ—¬ 디버깅에 μ‚¬μš©ν•©λ‹ˆλ‹€.
const obj = { name: 'John', age: 30 }; const str = util.inspect(obj);
JavaScript
볡사

μ˜ˆμ‹œ μ½”λ“œ

const util = require('util'); // 1. promisify const fs = require('fs'); const readFile = util.promisify(fs.readFile); readFile('example.txt', 'utf8') .then((data) => console.log('File content:', data)) .catch((error) => console.error('Error reading file:', error)); // 2. inherits function Base() { this.name = 'Base'; } function Child() { Base.call(this); this.name = 'Child'; } util.inherits(Child, Base); const childInstance = new Child(); console.log('Child name:', childInstance.name); // 3. format const message = util.format('%s %s', 'Hello', 'World'); console.log('Formatted message:', message); // 4. inspect const obj = { name: 'John', age: 30 }; const str = util.inspect(obj); console.log('Inspect result:', str);
JavaScript
볡사

μ‚¬μš© λͺ©μ 

β€’
Promise νŒ¨ν„΄μ„ μ‚¬μš©ν•˜κΈ° μ‰½κ²Œ λ§Œλ“œλŠ” promisify ν•¨μˆ˜.
β€’
클래슀 κ°„μ˜ 상속을 λ•λŠ” inherits ν•¨μˆ˜.
β€’
μ„œμ‹ λ¬Έμžμ—΄μ„ μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ λ§Œλ“€μ–΄λ‚΄λŠ” format ν•¨μˆ˜.
β€’
디버깅을 μœ„ν•΄ 객체λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜λŠ” inspect ν•¨μˆ˜.
util λͺ¨λ“ˆμ€ λ‹€μ–‘ν•œ μœ ν‹Έλ¦¬ν‹° ν•¨μˆ˜λ₯Ό μ œκ³΅ν•˜μ—¬ κ°œλ°œμžκ°€ 효율적으둜 μ½”λ“œλ₯Ό μž‘μ„±ν•˜κ³  디버깅할 수 μžˆλ„λ‘ λ•μŠ΅λ‹ˆλ‹€.