현재 사용하지 않는 기능으로 특정 관련 잇는 코드들을 하나의 모듈로 묶을 수 있는 방법이다.
class Idol{
name: string;
age: number;
constructor(name: string, age: number){
this.name = name;
this.age = age;
}
}
class User{
email : string;
name: string;
constructor(email: string, name: string){
this.email = email;
this.name = name;
}
}
Idol클래스와 User클래스는 서로 연관이 없다. 그래서 모듈로 관리하고 싶다.
namespace Home{
class Idol{
name: string;
age: number;
constructor(name: string, age: number){
this.name = name;
this.age = age;
}
}
export const yuJin = new Idol(
'안유진',
23,
) //다른 네임스페이스에서 불러올 수 있다.
}
// 같은 파일 안에 있어도 Idol클래스에 접근할 수 없다. 완전 독립된 공간이 된다.
namespace Post{
class User{
email : string;
name: string;
constructor(email: string, name: string){
this.email = email;
this.name = name;
}
}
new Idol() //에러
const admin = Home.yuJin // 불러올 수 있다.
}
Post namespace에서 같은 파일이여도 다른 namespace에 export되지 않는 것들은 불러올 수 없다. 그래서 Idol클래스 인스턴스를 생성할 수 없다.
하지만 export한 yuJin은 받아올 수 있다.
namespace Comment{
const name = 'comment';
namespace Detail{
const page = 'detail';
console.log(name);
console.log(page);
}
console.log('---------');
console.log(name);
// console.log(page); // 에러발생
}
scope는 외부에서 내부 변수를 가져올 수 없지만 내부에서 외부변수를 가져올 수 있다.
중요한 것은 더 이상 사용하지 않는다.