Skip to content

Udemy/Ts/section5/63: private with constructor #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 46 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d105911
🌱: README을 수정
4BFC Aug 26, 2024
06270b7
🌱: README을 수정
4BFC Aug 26, 2024
5e69dee
🌱: README을 수정
4BFC Aug 29, 2024
d7c1360
🌱: README을 수정
4BFC Aug 29, 2024
978d932
🌱: README을 수정
4BFC Aug 29, 2024
859bffc
🌱: README을 수정
4BFC Aug 29, 2024
e09d95d
🌱: README을 수정
4BFC Aug 29, 2024
3255009
🌱: README을 수정
4BFC Aug 29, 2024
ab9b8db
🌱: README을 수정
4BFC Aug 29, 2024
5f0f9f9
🌱: README을 수정
4BFC Aug 29, 2024
daaeeed
🌱: README을 수정
4BFC Aug 29, 2024
c8ec455
🌱: README을 수정
4BFC Aug 30, 2024
e2a15fd
🌱: README을 수정
4BFC Aug 30, 2024
61dd842
🌱: README을 수정
4BFC Aug 30, 2024
bce5c89
🌱: README을 수정
4BFC Aug 30, 2024
ed8fa00
🌱: issue templates 생성
4BFC Aug 30, 2024
1559fa2
🌱: pr-template 생성
4BFC Aug 30, 2024
691897a
🌱: pr-template 수정
4BFC Aug 30, 2024
44ec035
🌱: pr-template 수정
4BFC Aug 30, 2024
3c3280b
🚩: udemy section2의 14번 강의를 듣고 실습을 했다.
4BFC Aug 31, 2024
cbf04eb
🔀: Merge branch 'UdemyTs'
4BFC Aug 31, 2024
0f07929
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Aug 31, 2024
757fb99
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Aug 31, 2024
7e8ac7d
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Aug 31, 2024
aa89074
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
79304fe
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
eb21a7f
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
cf56f00
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
7dec10d
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
28200e9
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
621e700
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 2, 2024
5f9833d
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 2, 2024
06d966b
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 20, 2024
6dbfd8c
🚩: interface를 학습하기 위해 클래스를 학습하고 실습해보았다.
4BFC Sep 20, 2024
a63622c
🚩: dist의 컴파일된 js를 확인하기 위해서 es5에서 es6로 ts.config 변경
4BFC Sep 20, 2024
e0853e1
🚩: es6에서 es5로 컴파일 target을 변경
4BFC Sep 20, 2024
fc69d9e
🚩: this를 사용해서 출력한 class 내 전역 name 프로퍼티 정상 출력
4BFC Sep 20, 2024
32a0146
🚩: this키워드를 다시 한번 객체로 감싼 복제 함수 할당 후 출력 undefined 결과 확인, this를 사용시 주의 사항
4BFC Sep 20, 2024
61ff6bf
🚩: undefined 재확인
4BFC Sep 20, 2024
8416f2b
🚩: 복제한 class의 함수를 사용 가능하게 하기 위한 방법과 에러 임시적인 해결 단, 똑같이 구현 불가능
4BFC Sep 20, 2024
cbdaa30
🚩: 배열 프로퍼티에 접근하는 두 가지 방식
4BFC Sep 21, 2024
24ebd7d
🚩: private 설정으로 employees[N]에 값을 할당할수 없다. error
4BFC Sep 21, 2024
c7ed403
🚩: 런타임과 컴파일 과정의 private 영향을 ts, js 파일에서 확인
4BFC Sep 21, 2024
01311f2
🚩: private를 일반적으로 constructor에서 사용하는 방법
4BFC Sep 21, 2024
263c777
🚩: constructor 생성자 내부에 따로 선언하지 않고 매개변수로 선언하는 간단한 방법
4BFC Sep 21, 2024
e8a67fa
Merge branch 'UdemyTs' into Udemy/Ts/section5/63
4BFC Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions dist/app.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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();
accounting.printEmployeeInformation();
21 changes: 10 additions & 11 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -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();