diff --git a/dist/app.js b/dist/app.js index ed41e56..dbda66a 100644 --- a/dist/app.js +++ b/dist/app.js @@ -1,11 +1,16 @@ "use strict"; var Department = /** @class */ (function () { - function Department(n) { + function Department(id, name) { + this.id = id; + this.name = name; + // private id: string; + // private name: string; this.employees = []; - this.name = n; + // this.id = id; + // this.name = n } Department.prototype.describe = function () { - console.log("Department: " + this.name); + console.log("Department (".concat(this.id, "): ").concat(this.name)); }; Department.prototype.addEmployee = function (employee) { this.employees.push(employee); @@ -16,13 +21,9 @@ var Department = /** @class */ (function () { }; return Department; }()); -var accounting = new Department('Accounting'); +var accounting = new Department('1', '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.employees[2] = 'Anna'; accounting.describe(); -accounting.printEmployeeInformation(); \ No newline at end of file +accounting.printEmployeeInformation(); diff --git a/src/app.ts b/src/app.ts index 730b51a..158684d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,31 +1,30 @@ class Department { - name: string; + // private id: string; + // private name: string; private employees: string[] = []; - constructor(n: string) { - this.name = n; + + constructor(private id: string, public name: string) { + // this.id = id; + // this.name = n } describe(this: Department) { - console.log(`Department: ` + this.name) + console.log(`Department (${this.id}): ${this.name}`); } addEmployee(employee: string) { this.employees.push(employee); } - printEmployeeInformation() { console.log(this.employees.length); console.log(this.employees); } } -const accounting = new Department('Accounting'); +const accounting = new Department('1', '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.employees[2] = 'Anna'; accounting.describe(); accounting.printEmployeeInformation();