diff --git a/dist/app.js b/dist/app.js index e69329c..ed41e56 100644 --- a/dist/app.js +++ b/dist/app.js @@ -1,18 +1,28 @@ "use strict"; var Department = /** @class */ (function () { function Department(n) { + this.employees = []; this.name = n; } - Department.prototype.describe = function () { console.log("Department: " + this.name); }; + Department.prototype.addEmployee = function (employee) { + this.employees.push(employee); + }; + Department.prototype.printEmployeeInformation = function () { + console.log(this.employees.length); + console.log(this.employees); + }; return Department; }()); var accounting = new Department('Accounting'); +accounting.addEmployee('Max'); +accounting.addEmployee('Manu'); +accounting.employees[2] = 'Anna'; //employee private 설정으로 error 발생 +//위와 같은 방식으로 코드를 작성하고 실행해도 정상적으로 동작을 하지만 복잡한 코드에서 위와 같은 방식은 좋은 방식이 아니다. 따라서 employee를 접근할 수 없게 해야한다. 따라 접근 범위를 제한할 수 있는 private를 사용하면 된다. +//TSError: ⨯ Unable to compile TypeScript: src / app.ts: 26: 12 - error TS2341: Property 'employees' is private and only accessible within class 'Department'. +// private는 class 내에서만 접근이 가능하고 외부에서는 접근할수 없다. +// 단, 해당 error 부분은 컴파일할 때는 문제가 없이 동작해서 app.js에서 확인은 가능하지만 런타임 과정에서는 error를 발생시킨다. accounting.describe(); -var accountingCopy = { name: 'newName', describe: accounting.describe }; -//여기서 undefined를 출력하는 이유는 describe가 참조하는 것이 무엇인지 알 수 없기 때문이다. -//따라서 accountingCopy와 같이 새로운 객체로 재생성을 해서 할당하기 위해서는 class의 describe함수의 매개변수에 -//this를 넣고 타입지정을 Department로 설정한 뒤(즉, 청사진을 참조하게 설정) accountingCopy에 name 프로퍼티를 기입, 할당해줘야 한다. -accountingCopy.describe(); +accounting.printEmployeeInformation(); \ No newline at end of file diff --git a/package.json b/package.json index 3da0a6b..0d16423 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", + "watch": "tsc -w", "start": "ts-node src/app.ts", "build": "tsc --build", "clean": "tsc --build --clean" diff --git a/src/app.ts b/src/app.ts index 0836e54..730b51a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,20 +1,31 @@ class Department { name: string; + private employees: string[] = []; constructor(n: string) { this.name = n; } - describe(this: Department) { console.log(`Department: ` + this.name) } + + addEmployee(employee: string) { + this.employees.push(employee); + } + + printEmployeeInformation() { + console.log(this.employees.length); + console.log(this.employees); + } } const accounting = new Department('Accounting'); -accounting.describe(); +accounting.addEmployee('Max'); +accounting.addEmployee('Manu'); -const accountingCopy = { name: 'newName', describe: accounting.describe }; -//여기서 undefined를 출력하는 이유는 describe가 참조하는 것이 무엇인지 알 수 없기 때문이다. -//따라서 accountingCopy와 같이 새로운 객체로 재생성을 해서 할당하기 위해서는 class의 describe함수의 매개변수에 -//this를 넣고 타입지정을 Department로 설정한 뒤(즉, 청사진을 참조하게 설정) accountingCopy에 name 프로퍼티를 기입, 할당해줘야 한다. - -accountingCopy.describe(); \ No newline at end of file +accounting.employees[2] = 'Anna'; //employee private 설정으로 error 발생 +//위와 같은 방식으로 코드를 작성하고 실행해도 정상적으로 동작을 하지만 복잡한 코드에서 위와 같은 방식은 좋은 방식이 아니다. 따라서 employee를 접근할 수 없게 해야한다. 따라 접근 범위를 제한할 수 있는 private를 사용하면 된다. +//TSError: ⨯ Unable to compile TypeScript: src / app.ts: 26: 12 - error TS2341: Property 'employees' is private and only accessible within class 'Department'. +// private는 class 내에서만 접근이 가능하고 외부에서는 접근할수 없다. +// 단, 해당 error 부분은 컴파일할 때는 문제가 없이 동작해서 app.js에서 확인은 가능하지만 런타임 과정에서는 error를 발생시킨다. +accounting.describe(); +accounting.printEmployeeInformation();