프론트엔드 센트럴파크 (☞゚ヮ゚)☞

commonJS로 JS파일 연동하기 본문

Javascript

commonJS로 JS파일 연동하기

자라나라나무나무나 2022. 7. 27. 18:11

주는 파일(Export)

/**
 * CommonJS (Export)
 */
function Person(name, age, location) {
	this.name = name;
	this.age = age;
	this.location = location;

	this.getName = function () {
		return this.name + '입니다';
	};
}

module.exports = Person;

 

받는 파일(Import)

- require 을 사용한다.

/**
 * CommonJS (Import)
 */
const Person = require('./02-CommonJS-person');

const me = new Person('jang', 10, 'Korea');
const you = new Person('kim', 20, 'China');

console.log(me.getName());
console.log(you.getName());
Comments