μΉ΄ν…Œκ³ λ¦¬ 보관물: Javascript

Javascript

TypeScript μ™ΈλΆ€ λͺ¨λ“ˆκ³Ό ν•¨κ»˜ λ„€μž„ 슀페이슀λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ μ–΄λ–»κ²Œν•©λ‹ˆκΉŒ? /* … */ } } } dog.ts import

μ½”λ“œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

baseTypes.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}

dog.ts

import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}

tree.ts

// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}

이것은 λͺ¨λ‘ 맀우 ν˜Όλž€ μŠ€λŸ½λ‹€. μ™ΈλΆ€ λͺ¨λ“ˆμ„ λͺ¨λ‘ μ‚¬μš©ν•˜μ—¬ μœ ν˜•μ„ λ™μΌν•œ λ„€μž„ μŠ€νŽ˜μ΄μŠ€μ— κΈ°μ—¬ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€ Living.Things. 이 κΈ°λŠ₯이 μ „ν˜€ μž‘λ™ν•˜μ§€ μ•ŠλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€ . Animalμ—μ„œ λ³Ό 수 μ—†μŠ΅λ‹ˆλ‹€ dogs.ts. λ‚˜λŠ” 전체 λ„€μž„ 슀페이슀 이름을 μ“Έ ν•„μš”κ°€ b.Living.Things.Plant의λ₯Ό tree.ts. νŒŒμΌμ—μ„œ λ™μΌν•œ λ„€μž„ 슀페이슀의 μ—¬λŸ¬ 개체λ₯Ό κ²°ν•©ν•˜λŠ” 것은 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ–΄λ–»κ²Œν•΄μ•Όν•©λ‹ˆκΉŒ?



λ‹΅λ³€

μΊ”λ”” μ»΅ μœ μΆ”

버전 1 : λͺ¨λ“  μ‚¬νƒ•μ„μœ„ν•œ μ»΅

λ‹€μŒκ³Ό 같은 μ½”λ“œλ₯Ό μž‘μ„±ν–ˆλ‹€κ³  κ°€μ • ν•΄ λ΄…μ‹œλ‹€.

Mod1.ts

export namespace A {
    export class Twix { ... }
}

Mod2.ts

export namespace A {
    export class PeanutButterCup { ... }
}

Mod3.ts

export namespace A {
     export class KitKat { ... }
}

이 섀정을 λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€ :

각 λͺ¨λ“ˆ (μš©μ§€) μ—λŠ” 이름이 μ§€μ •λœ 자체 μ»΅ 이 AμžˆμŠ΅λ‹ˆλ‹€. 이것은 μ“Έλͺ¨κ°€ μ—†μŠ΅λ‹ˆλ‹€-μ‹€μ œλ‘œ 사탕을 쑰직 ν•˜λŠ” 것이 μ•„λ‹ˆλΌ, λ‹Ήμ‹ κ³Ό 간식 사이에 μΆ”κ°€ 단계 (μ»΅μ—μ„œ κΊΌλ‚΄κΈ°)λ₯Ό μΆ”κ°€ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.


버전 2 : κΈ€λ‘œλ²Œ λ²”μœ„μ—μ„œ ν•œ μ»΅

λͺ¨λ“ˆμ„ μ‚¬μš©ν•˜μ§€ μ•Šμ•˜λ‹€λ©΄ λ‹€μŒκ³Ό 같은 μ½”λ“œλ₯Ό μž‘μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€ ( exportμ„ μ–Έ λΆ€μ‘± ).

global1.ts

namespace A {
    export class Twix { ... }
}

global2.ts

namespace A {
    export class PeanutButterCup { ... }
}

global3.ts

namespace A {
     export class KitKat { ... }
}

이 μ½”λ“œ AλŠ” μ „μ—­ λ²”μœ„μ—μ„œ 병합 된 λ„€μž„ 슀페이슀 λ₯Ό λ§Œλ“­λ‹ˆλ‹€ .

이 섀정은 μœ μš©ν•˜μ§€λ§Œ λͺ¨λ“ˆμ˜ κ²½μš°μ—λŠ” μ μš©λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ (λͺ¨λ“ˆμ΄ μ „μ—­ λ²”μœ„λ₯Ό μ˜€μ—Όμ‹œν‚€μ§€ μ•ŠκΈ° λ•Œλ¬Έμ—).


버전 3 : 컡리슀

μ›λž˜μ˜ 예λ₯Ό λ‹€μ‹œκ°€λŠ”, μ»΅ A, A및A λ‹Ήμ‹ μ—κ²Œ 호의λ₯Όν•˜κ³  μžˆμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λŒ€μ‹  μ½”λ“œλ₯Ό λ‹€μŒκ³Ό 같이 μž‘μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Mod1.ts

export class Twix { ... }

Mod2.ts

export class PeanutButterCup { ... }

Mod3.ts

export class KitKat { ... }

λ‹€μŒκ³Ό 같은 그림을 λ§Œλ“€λ €λ©΄

훨씬 λ‚«λ‹€!

이제 λͺ¨λ“ˆμ—μ„œ λ„€μž„ 슀페이슀λ₯Ό μ–Όλ§ˆλ‚˜ 많이 μ‚¬μš©ν•˜κ³  싢은지 아직도 μƒκ°ν•˜κ³  μžˆλ‹€λ©΄ 계속 μ½μœΌμ‹­μ‹œμ˜€ …


이듀은 당신이 μ°Ύκ³ μžˆλŠ” κ°œλ…μ΄ μ•„λ‹™λ‹ˆλ‹€

λ„€μž„ μŠ€νŽ˜μ΄μŠ€κ°€ 처음 μ‘΄μž¬ν•˜λŠ” 이유의 κ·Όμ›μœΌλ‘œ λŒμ•„κ°€μ„œ μ΄λŸ¬ν•œ μ΄μœ κ°€ μ™ΈλΆ€ λͺ¨λ“ˆμ— μ ν•©ν•œ μ§€ μ—¬λΆ€λ₯Ό μ‘°μ‚¬ν•΄μ•Όν•©λ‹ˆλ‹€.

ꡬ성 : λ„€μž„ μŠ€νŽ˜μ΄μŠ€λŠ” λ…Όλ¦¬μ μœΌλ‘œ κ΄€λ ¨λœ 객체와 μœ ν˜•μ„ κ·Έλ£Ήν™”ν•˜λŠ” 데 νŽΈλ¦¬ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ C #μ—μ„œλŠ” λͺ¨λ“  μ»¬λ ‰μ…˜ μœ ν˜•μ„System.Collections . μœ ν˜•μ„ 계측 적 λ„€μž„ 슀페이슀둜 κ΅¬μ„±ν•˜μ—¬ ν•΄λ‹Ή μœ ν˜•μ˜ μ‚¬μš©μžμ—κ²Œ 쒋은 β€œλ°œκ²¬β€ν™˜κ²½μ„ μ œκ³΅ν•©λ‹ˆλ‹€.

이름 좩돌 : 이름 곡간은 이름 μΆ©λŒμ„ ν”Όν•˜κΈ° μœ„ν•΄ μ€‘μš”ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, 당신은 ν•  수 My.Application.Customer.AddForm와 My.Application.Order.AddForm두 개의 λ™μΌν•œ 이름을 κ°€μ§„ μœ ν˜•,ν•˜μ§€λ§Œ λ‹€λ₯Έ λ„€μž„ 슀페이슀λ₯Ό -. λͺ¨λ“  μ‹λ³„μžκ°€ λ™μΌν•œ 루트 λ²”μœ„μ— 있고 λͺ¨λ“  μ–΄μ…ˆλΈ”λ¦¬κ°€ λͺ¨λ“  μœ ν˜•μ„λ‘œλ“œν•˜λŠ” μ–Έμ–΄μ—μ„œλŠ” λͺ¨λ“  것이 λ„€μž„ μŠ€νŽ˜μ΄μŠ€μ— μžˆμ–΄μ•Όν•©λ‹ˆλ‹€.

μ΄λŸ¬ν•œ μ΄μœ λŠ” μ™ΈλΆ€ λͺ¨λ“ˆμ—μ„œ μ˜λ―Έκ°€ μžˆμŠ΅λ‹ˆκΉŒ?

ꡬ성 : μ™ΈλΆ€ λͺ¨λ“ˆμ€ 이미 파일 μ‹œμŠ€ν…œμ— μ‘΄μž¬ν•©λ‹ˆλ‹€. κ²½λ‘œμ™€ 파일 μ΄λ¦„μœΌλ‘œ ν•΄κ²°ν•΄μ•Όν•˜λ―€λ‘œ μ‚¬μš©ν•  μˆ˜μžˆλŠ” 논리적 ꡬ성 체계가 μžˆμŠ΅λ‹ˆλ‹€. μš°λ¦¬λŠ” /collections/generic/폴더λ₯Ό κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€listλͺ¨λ“ˆ .

이름 좩돌 : 이것은 μ™ΈλΆ€ λͺ¨λ“ˆμ— μ „ν˜€ μ μš©λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λͺ¨λ“ˆ λ‚΄ μ—μ„œ 같은 이름을 κ°€μ§„ 두 개의 객체λ₯Ό κ°€μ§ˆλ§Œν•œ μ΄μœ λŠ” μ—†μŠ΅λ‹ˆλ‹€. μ†ŒλΉ„ μΈ‘λ©΄μ—μ„œ νŠΉμ • λͺ¨λ“ˆ 의 μ†ŒλΉ„μž λŠ” λͺ¨λ“ˆμ„ μ°Έμ‘°ν•˜λŠ” 데 μ‚¬μš©ν•  이름을 μ„ νƒν•˜λ―€λ‘œ μš°μ—°ν•œ 이름 좩돌이 λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€.


λͺ¨λ“ˆμ΄ μž‘λ™ν•˜λŠ” λ°©μ‹μœΌλ‘œ μ΄λŸ¬ν•œ μ΄μœ κ°€ 적절히 ν•΄κ²°λ˜μ—ˆλ‹€κ³  μƒκ°ν•˜μ§€ μ•Šλ”λΌλ„ μ™ΈλΆ€ λͺ¨λ“ˆμ—μ„œ λ„€μž„ 슀페이슀λ₯Ό μ‚¬μš©ν•˜λ €λŠ” β€œμ†”λ£¨μ…˜β€μ€ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

μƒμžμ— μƒμžμ— μƒμž

이야기:

친ꡬ Bob이 μ „ν™”ν•©λ‹ˆλ‹€. β€œμ €λŠ” 집에 μ•„μ£Ό ν›Œλ₯­ν•œ 쑰직 체계가 μžˆμŠ΅λ‹ˆλ‹€.” κΉ”λ”ν•œ, λ°₯이 무슨 μΌμ„ν–ˆλŠ”μ§€ β€‹β€‹λ³΄μž.

λΆ€μ—Œμ—μ„œ μ‹œμž‘ν•˜μ—¬ μ‹λ£Œν’ˆ μ €μž₯μ‹€μ„μ—¬μ‹­μ‹œμ˜€. 각각 β€œPantry”라고 λ ˆμ΄λΈ”μ΄ μ§€μ •λœ 60 개의 μ„œλ‘œ λ‹€λ₯Έ μƒμžκ°€ μžˆμŠ΅λ‹ˆλ‹€. μƒμžλ₯Ό λ¬΄μž‘μœ„λ‘œ κ³¨λΌμ„œ μ—½λ‹ˆ λ‹€. λ‚΄λΆ€μ—λŠ” β€œκ³‘λ¬Όβ€μ΄λΌλŠ” 단일 μƒμžκ°€ μžˆμŠ΅λ‹ˆλ‹€. β€œκ³‘λ¬Όβ€μƒμžλ₯Ό μ—΄κ³  β€œνŒŒμŠ€νƒ€β€λΌκ³  ν‘œμ‹œλœ 단일 μƒμžλ₯Ό μ°ΎμœΌμ‹­μ‹œμ˜€. β€œνŒŒμŠ€νƒ€β€μƒμžλ₯Ό μ—΄κ³  β€œνŽœβ€μ΄λΌλŠ” 단일 μƒμžλ₯Ό μ°ΎμœΌμ‹­μ‹œμ˜€. 이 μƒμžλ₯Ό μ—΄κ³  μ˜ˆμƒλŒ€λ‘œ νŽœλ„€ νŒŒμŠ€νƒ€ ν•œ 봉지λ₯Ό μ°ΎμœΌμ‹­μ‹œμ˜€.

μ•½κ°„ ν˜Όλ™λ˜λ©΄ β€œPantry”라고 ν‘œμ‹œλœ 인접 μƒμžλ₯Ό μ„ νƒν•˜μ‹­μ‹œμ˜€. μ•ˆμ—λŠ” β€œκ³‘λ¬Όβ€μ΄λΌλŠ” λ ˆμ΄λΈ”μ΄ 뢙은 단일 μƒμžκ°€ μžˆμŠ΅λ‹ˆλ‹€. β€œκ³‘λ¬Όβ€μƒμžλ₯Ό μ—΄κ³  β€œνŒŒμŠ€νƒ€β€λΌκ³  ν‘œμ‹œλœ 단일 μƒμžλ₯Ό λ‹€μ‹œ μ°ΎμŠ΅λ‹ˆλ‹€. β€œνŒŒμŠ€νƒ€β€μƒμžλ₯Ό μ—΄κ³  ν•˜λ‚˜μ˜ μƒμžλ₯Ό μ°ΎμœΌμ‹­μ‹œμ˜€.이 μƒμžμ—λŠ” β€œRigatoniβ€λΌλŠ” λ ˆμ΄λΈ”μ΄ λΆ™μ–΄ μžˆμŠ΅λ‹ˆλ‹€. 이 μƒμžλ₯Ό μ—΄κ³  리가 ν† λ‹ˆ νŒŒμŠ€νƒ€ ν•œ 봉지λ₯Ό μ°ΎμœΌμ„Έμš”

β€œμ’‹μ•„μš”!” λ°₯이 λ§ν•©λ‹ˆλ‹€. β€œλͺ¨λ“  것이 λ„€μž„ μŠ€νŽ˜μ΄μŠ€μ— μžˆμŠ΅λ‹ˆλ‹€!”.

β€œν•˜μ§€λ§Œ λ°₯ …”당신이 λŒ€λ‹΅ν•©λ‹ˆλ‹€. β€œμ‘°μ§ ꡬ성은 μ“Έλͺ¨κ°€ μ—†μŠ΅λ‹ˆλ‹€. 무엇이든 μ–»κΈ° μœ„ν•΄ λ§Žμ€ μƒμžλ₯Ό μ—΄μ–΄μ•Όν•©λ‹ˆλ‹€. μ‹€μ œλ‘œ μ„Έ κ°œκ°€ μ•„λ‹Œ ν•œ μƒμž 에 λͺ¨λ“  것을 넣은 것보닀 더 μ‰½κ²Œ 찾을 μˆ˜λŠ” μ—†μŠ΅λ‹ˆλ‹€. . μ‹€μ œλ‘œ, μ‹λ£Œν’ˆ μ €μž₯싀은 이미 μ„ λ°˜λ³„λ‘œ λΆ„λ₯˜λ˜μ–΄ μžˆμœΌλ―€λ‘œ μƒμžκ°€ μ „ν˜€ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ„ λ°˜μ— νŒŒμŠ€νƒ€λ₯Ό 놓고 ν•„μš”ν•  λ•Œ μ§‘μ–΄ μ˜¬λ¦¬λŠ” 것이 μ–΄λ–»μŠ΅λ‹ˆκΉŒ? β€œ

β€œλ‹Ήμ‹ μ€ μ΄ν•΄ν•˜μ§€ λͺ»ν•©λ‹ˆλ‹€-λ‹€λ₯Έ λˆ„κ΅¬λ„ β€˜Pantryβ€™λ„€μž„ μŠ€νŽ˜μ΄μŠ€μ— μ†ν•˜μ§€ μ•Šμ€ 것을 λ„£μ§€ μ•Šλ„λ‘ν•΄μ•Όν•©λ‹ˆλ‹€. 그리고 λͺ¨λ“  νŒŒμŠ€νƒ€λ₯Ό Pantry.Grains.Pastaλ„€μž„ μŠ€νŽ˜μ΄μŠ€μ— μ•ˆμ „ν•˜κ²Œ μ •λ¦¬ν•˜μ—¬ μ‰½κ²Œ 찾을 수 μžˆμŠ΅λ‹ˆλ‹€.”

λ°₯은 맀우 ν˜Όλž€μŠ€λŸ¬μš΄ μ‚¬λžŒμž…λ‹ˆλ‹€.

λͺ¨λ“ˆμ€ 자체 μƒμžμž…λ‹ˆλ‹€

μ•„λ§ˆλ„ μ‹€μ œ μƒν™©μ—μ„œ λΉ„μŠ·ν•œ 일이 μžˆμ—ˆμ„ κ²ƒμž…λ‹ˆλ‹€. μ•„λ§ˆμ‘΄μ—μ„œ λͺ‡ κ°€μ§€ 물건을 μ£Όλ¬Έν•˜λ©΄ 각 μƒμžμ— μž‘μ€ μƒμžκ°€ 있고 μƒμžμ— 포μž₯λ˜μ–΄μžˆλŠ” 각 μƒμžμ— 자체 ν•­λͺ©μ΄ ν‘œμ‹œλ©λ‹ˆλ‹€. λ‚΄λΆ€ μƒμžκ°€ μœ μ‚¬ν•˜λ”λΌλ„ λ°œμ†‘λ¬Όμ€ μœ μš©ν•˜κ²Œ β€œκ²°ν•©β€λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ°•μŠ€ μœ μΆ”μ™€ λ§ˆμ°¬κ°€μ§€λ‘œ μ£Όμš” κ΄€μ°° 사항은 μ™ΈλΆ€ λͺ¨λ“ˆμ΄ 자체 λ°•μŠ€λΌλŠ” 것 μž…λ‹ˆλ‹€. λ§Žμ€ κΈ°λŠ₯을 κ°€μ§„ 맀우 λ³΅μž‘ν•œ ν•­λͺ© 일 수 μžˆμ§€λ§Œ μ§€μ •λœ μ™ΈλΆ€ λͺ¨λ“ˆμ€ 자체 μƒμžμž…λ‹ˆλ‹€.


μ™ΈλΆ€ λͺ¨λ“ˆμ— λŒ€ν•œ μ§€μΉ¨

이제 β€˜λ„€μž„ μŠ€νŽ˜μ΄μŠ€β€™λ₯Ό μ‚¬μš©ν•  ν•„μš”κ°€ μ—†λ‹€λŠ” 것을 μ•Œμ•˜μœΌλ―€λ‘œ λͺ¨λ“ˆμ„ μ–΄λ–»κ²Œ κ΅¬μ„±ν•΄μ•Όν•©λ‹ˆκΉŒ? λͺ‡ κ°€μ§€ μ§€μΉ¨ 원칙과 μ˜ˆκ°€ μ΄μ–΄μ§‘λ‹ˆλ‹€.

κ°€λŠ₯ν•œ ν•œ μ΅œμƒμœ„ μˆ˜μ€€μ— κ°€κΉκ²Œ 내보내기

  • 단일 클래슀 λ˜λŠ” ν•¨μˆ˜ 만 λ‚΄λ³΄λ‚΄λŠ” 경우 λ‹€μŒμ„ μ‚¬μš©ν•˜μ‹­μ‹œμ˜€ export default.

MyClass.ts

export default class SomeType {
  constructor() { ... }
}

MyFunc.ts

function getThing() { return 'thing'; }
export default getThing;

μ†ŒλΉ„

import t from './MyClass';
import f from './MyFunc';
var x = new t();
console.log(f());

이것은 μ†ŒλΉ„μžμ—κ²Œ μ΅œμ μž…λ‹ˆλ‹€. 그듀은 μ›ν•˜λŠ”λŒ€λ‘œ ( t이 경우) λ‹Ήμ‹ μ˜ μœ ν˜•μ˜ 이름을 μ§€μ •ν•  수 있으며 물체λ₯Ό μ°ΎκΈ° μœ„ν•΄ λΆˆν•„μš”ν•œ λ„νŒ…μ„ ν•  ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€.

  • μ—¬λŸ¬ 객체λ₯Ό λ‚΄λ³΄λ‚΄λŠ” 경우 λͺ¨λ‘ μ΅œμƒμœ„ λ ˆλ²¨μ— λ‘μ‹­μ‹œμ˜€.

MyThings.ts

export class SomeType { ... }
export function someFunc() { ... }

μ†ŒλΉ„

import * as m from './MyThings';
var x = new m.SomeType();
var y = m.someFunc();
  • λ§Žμ€ 것을 λ‚΄λ³΄λ‚΄λŠ” κ²½μš°μ—λ§Œ module/ namespaceν‚€μ›Œλ“œ λ₯Ό μ‚¬μš©ν•΄μ•Όν•©λ‹ˆλ‹€ .

MyLargeModule.ts

export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}

μ†ŒλΉ„

import { Animals, Plants} from './MyLargeModule';
var x = new Animals.Dog();

뢉은 κΉƒλ°œ

λ‹€μŒμ€ λͺ¨λ‘ λͺ¨λ“ˆ ꡬ쑰화λ₯Όμœ„ν•œ μœ„ν—˜ μ‹ ν˜Έμž…λ‹ˆλ‹€. λ‹€μŒ 쀑 ν•˜λ‚˜λΌλ„ νŒŒμΌμ— μ μš©λ˜λŠ” 경우 μ™ΈλΆ€ λͺ¨λ“ˆμ˜ λ„€μž„ 슀페이슀λ₯Ό λ§Œλ“€μ§€ μ•ŠλŠ”μ§€ λ‹€μ‹œ ν™•μΈν•˜μ‹­μ‹œμ˜€.

  • μ΅œμƒμœ„ μ„ μ–Έ λ§ŒμžˆλŠ” 파일 export module Foo { ... }( Fooλͺ¨λ“  것을 제거 ν•˜κ³  레벨 μœ„λ‘œ 이동)
  • 단일 파일이 export classμžˆκ±°λ‚˜ export functionκ·Έλ ‡μ§€ μ•Šμ€ 파일export default
  • export module Foo {μ΅œμƒμœ„ μˆ˜μ€€μ—μ„œ λ™μΌν•œ μ—¬λŸ¬ 파일 (이 νŒŒμΌλ“€μ΄ ν•˜λ‚˜λ‘œ κ²°ν•© 될 것이라고 μƒκ°ν•˜μ§€ λ§ˆμ‹­μ‹œμ˜€ Foo!)

λ‹΅λ³€

아무것도 λΌμ΄μ–Έμ˜ λŒ€λ‹΅μ€ 잘λͺ»ν•˜μ§€λ§Œ μœ μ§€ κ΄€λ¦¬ν•˜λŠ” 방법을 μ°Ύκ³  μ—¬κΈ° 온 μ‚¬λžŒλ“€μ„μœ„ν•œ ν•˜λ‚˜μ˜ 클래슀 λ‹Ή 파일 μ—¬μ „νžˆ μ œλŒ€λ‘œμ„ μ°Έμ‘°ν•˜μ‹œκΈ° λ°”λžλ‹ˆλ‹€ ES6 λ„€μž„ 슀페이슀λ₯Ό μ‚¬μš©ν•˜λŠ” λ™μ•ˆ ꡬ쑰λ₯Ό 이 Microsoftμ—μ„œ 유용 μžμ›.

ν•΄λ‹Ή λ¬Έμ„œλ₯Ό 읽은 ν›„ λ‚˜μ—κ²Œ λΆˆλΆ„λͺ… ν•œ κ°€μ§€μž…λ‹ˆλ‹€ : 전체λ₯Ό κ°€μ Έ μ˜€λŠ” λ°©λ²•μœΌλ‘œ λͺ¨λ“ˆ (합병) ν•˜λ‚˜ import .


이 닡변을 μ—…λ°μ΄νŠΈν•˜λ €λ©΄ μˆœν™˜μ„ λ‹€μ‹œ νŽΈμ§‘ν•˜μ‹­μ‹œμ˜€ . λ„€μž„ μŠ€νŽ˜μ΄μŠ€μ— λŒ€ν•œ λͺ‡ κ°€μ§€ μ ‘κ·Ό 방식이 TS에 λ“±μž₯ν•©λ‹ˆλ‹€.

ν•˜λ‚˜μ˜ νŒŒμΌμ—μžˆλŠ” λͺ¨λ“  λͺ¨λ“ˆ 클래슀.

export namespace Shapes {
    export class Triangle {}
    export class Square {}
}

λ„€μž„ 슀페이슀둜 파일 κ°€μ Έ 였기 및 재 ν• λ‹Ή

import { Triangle as _Triangle } from './triangle';
import { Square as _Square } from './square';

export namespace Shapes {
  export const Triangle = _Triangle;
  export const Square = _Square;
}

배럴

// ./shapes/index.ts
export { Triangle } from './triangle';
export { Square } from './square';

// in importing file:
import * as Shapes from './shapes/index.ts';
// by node module convention, you can ignore '/index.ts':
import * as Shapes from './shapes';
let myTriangle = new Shapes.Triangle();

λ§ˆμ§€λ§‰ κ³ λ € 사항. 당신은 μˆ˜μžˆλŠ” 각 νŒŒμΌμ„ λ„€μž„ 슀페이슀

// triangle.ts
export namespace Shapes {
    export class Triangle {}
}

// square.ts
export namespace Shapes {
    export class Square {}
}

κ·ΈλŸ¬λ‚˜ λ™μΌν•œ λ„€μž„ μŠ€νŽ˜μ΄μŠ€μ—μ„œ 두 개의 클래슀λ₯Ό κ°€μ Έ 였면 TSλŠ” 쀑볡 μ‹λ³„μžκ°€ μžˆλ‹€κ³  λΆˆν‰ν•©λ‹ˆλ‹€. μ΄λ•Œ μœ μΌν•œ 해결책은 λ„€μž„ 슀페이슀의 별칭을 μ§€μ •ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

import { Shapes } from './square';
import { Shapes as _Shapes } from './triangle';

// ugh
let myTriangle = new _Shapes.Shapes.Triangle();

이 앨리어싱은 μ ˆλŒ€μ μœΌλ‘œ λ”μ°ν•˜κΈ° λ•Œλ¬Έμ— κ·Έλ ‡κ²Œν•˜μ§€ λ§ˆμ‹­μ‹œμ˜€. μœ„μ˜ μ ‘κ·Ό 방식을 μ‚¬μš©ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. 개인적으둜 λ‚˜λŠ” β€˜λ°°λŸ΄β€™μ„ μ„ ν˜Έν•©λ‹ˆλ‹€.


λ‹΅λ³€

ν΄λ”λ³„λ‘œ μ •λ¦¬ν•΄λ³΄μ‹­μ‹œμ˜€.

baseTypes.ts

export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}

dog.ts

import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   

tree.ts

import b = require('./baseTypes');

class Tree extends b.Plant {
}

LivingThings.ts

import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}

main.ts

import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)

μ•„μ΄λ””μ–΄λŠ” λͺ¨λ“ˆ μžμ²΄κ°€ λ„€μž„ μŠ€νŽ˜μ΄μŠ€μ— μ°Έμ—¬ν•˜κ³  μžˆκ±°λ‚˜ μ‹ κ²½ μ“°μ§€ μ•Šμ•„μ•Όν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 이것은 ν”„λ‘œμ νŠΈμ— μ‚¬μš©μ€‘μΈ λͺ¨λ“ˆ μ‹œμŠ€ν…œ μœ ν˜•μ— 관계없이 μž‘κ³  합리적인 λ°©μ‹μœΌλ‘œ APIλ₯Ό μ†ŒλΉ„μžμ—κ²Œ λ…ΈμΆœμ‹œν‚΅λ‹ˆλ‹€.


λ‹΅λ³€

Albinofrenchy λ‹΅λ³€μ˜ μž‘μ€ μ‹œν–‰ :

base.ts

export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}

dog.ts

import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 

things.ts

import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;

main.ts

import * as things from './things';

console.log(things.dog);

λ‹΅λ³€

OP λ‚˜λŠ” λ‹Ήμ‹ κ³Ό ν•¨κ»˜ μžˆμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ ν•œ 번, 300 개 μ΄μƒμ˜ νˆ¬ν‘œλ‘œ κ·Έ λŒ€λ‹΅μ—λŠ” μ•„λ¬΄λŸ° λ¬Έμ œκ°€ μ—†μ§€λ§Œ λ‚΄ μ˜κ²¬μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

  1. 클래슀λ₯Ό μ•„λŠ‘ν•œ λ”°λœ»ν•œ νŒŒμΌμ— κ°œλ³„μ μœΌλ‘œ λ„£λŠ” 것은 μ–΄λ–€ λ¬Έμ œμž…λ‹ˆκΉŒ? 이것이 더 잘 보이게 ν•  κ²ƒμž…λ‹ˆκΉŒ? (λ˜λŠ” λͺ¨λ“  λͺ¨λΈμ—μ„œ 1000 쀄 νŒŒμΌμ„ μ’‹μ•„ν•˜λŠ” μ‚¬λžŒ)

  2. λ”°λΌμ„œ 첫 번째 νŒŒμΌμ„ κ°€μ Έ 였렀면 import import import … man, srsly, model 파일, .d.ts 파일과 같은 각 λͺ¨λΈ νŒŒμΌμ—μ„œ importλ₯Ό κ°€μ Έμ™€μ•Όν•©λ‹ˆλ‹€. 거기에? κ°„λ‹¨ν•˜κ³  κΉ”λ”ν•΄μ•Όν•©λ‹ˆλ‹€. μˆ˜μž…ν’ˆμ΄ ν•„μš”ν•œ μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ? μ™œ? C #μ—μ„œ λ„€μž„ 슀페이슀λ₯Ό μ–»μ—ˆμŠ΅λ‹ˆλ‹€.

  3. 그리고 κ·Έλ•ŒκΉŒμ§€ 문자 κ·ΈλŒ€λ‘œ β€œfilenames.ts”λ₯Ό μ‹λ³„μžλ‘œ μ‚¬μš©ν•©λ‹ˆλ‹€. μ‹λ³„μžλ‘œμ„œ … 2017 년에 μ™€μ„œ 아직도 κ·Έλ ‡κ²Œν•©λ‹ˆκΉŒ? μ΄λ§ˆλŠ” ν™”μ„±μœΌλ‘œ λŒμ•„κ°€μ„œ 1000 λ…„ 더 μž μ„ μž”λ‹€.

μŠ¬ν”„κ²Œλ„ λ‚΄ λŒ€λ‹΅μ€ : nop, λͺ¨λ“  κ°€μ Έ 였기λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ±°λ‚˜ ν•΄λ‹Ή 파일 이름을 μ‹λ³„μžλ‘œ μ‚¬μš©ν•˜λŠ” 경우 β€œλ„€μž„ μŠ€νŽ˜μ΄μŠ€β€λ₯Ό κΈ°λŠ₯적으둜 λ§Œλ“€ 수 μ—†μŠ΅λ‹ˆλ‹€ (μ‹€μ œλ‘œ 바보라고 μƒκ°ν•©λ‹ˆλ‹€). 또 λ‹€λ₯Έ μ˜΅μ…˜μ€ μ΄λŸ¬ν•œ λͺ¨λ“  쒅속성을 filenameasidentifier.tsλΌλŠ” μƒμžμ— λ„£κ³  μ‚¬μš©ν•©λ‹ˆλ‹€.

export namespace(or module) boxInBox {} .

λ‹¨μˆœνžˆ 클래슀 μœ„μ—μžˆλŠ” μ°Έμ‘°λ₯Ό μ–»μœΌλ €κ³  ν•  λ•Œ 같은 μ΄λ¦„μœΌλ‘œ λ‹€λ₯Έ ν΄λž˜μŠ€μ— μ•‘μ„ΈμŠ€ν•˜λ €κ³ ν•˜μ§€ μ•Šλ„λ‘ λž©ν•‘ν•˜μ‹­μ‹œμ˜€.


λ‹΅λ³€

이 μ£Όμ œμ™€ κ΄€λ ¨ν•˜μ—¬ λ³Έ λͺ‡ κ°€μ§€ 질문 / μ˜κ²¬μ€ 마치 μ‚¬λžŒμ΄ Namespaceβ€˜λͺ¨λ“ˆ 별칭’을 의미 ν•˜λŠ” κ³³μ—μ„œ μ‚¬μš© ν•˜λŠ” κ²ƒμ²˜λŸΌ λ“€λ¦½λ‹ˆλ‹€ . Ryan Cavanaughκ°€ μžμ‹ μ˜ 의견 쀑 ν•˜λ‚˜μ—μ„œ μ–ΈκΈ‰ν–ˆλ“―μ΄ β€˜λž˜νΌβ€™λͺ¨λ“ˆλ‘œ μ—¬λŸ¬ λͺ¨λ“ˆμ„ λ‹€μ‹œ 내보낼 수 μžˆμŠ΅λ‹ˆλ‹€.

λ™μΌν•œ λͺ¨λ“ˆ 이름 / λ³„μΉ­μ—μ„œ λͺ¨λ‘ κ°€μ Έ 였렀면 래퍼 λͺ¨λ“ˆμ„μ˜ 경둜 λ§€ν•‘κ³Ό κ²°ν•©ν•˜μ‹­μ‹œμ˜€ tsconfig.json.

예:

./path/to/CompanyName.Products/Foo.ts

export class Foo {
    ...
}

./path/to/CompanyName.Products/Bar.ts

export class Bar {
    ...
}

./path/to/CompanyName.Products/index.ts

export { Foo } from './Foo';
export { Bar } from './Bar';

tsconfig.json

{
    "compilerOptions": {
        ...
        paths: {
            ...
            "CompanyName.Products": ["./path/to/CompanyName.Products/index"],
            ...
        }
        ...
    }
    ...
}

main.ts

import { Foo, Bar } from 'CompanyName.Products'

μ°Έκ³  : 좜λ ₯ .js 파일의 λͺ¨λ“ˆ ν•΄μƒλ„λŠ” https://github.com/tleunen/babel-plugin-module-resolver 와 같이 μ–΄λ–»κ²Œ λ“  μ²˜λ¦¬ν•΄μ•Όν•©λ‹ˆλ‹€.

.babelrc별λͺ… 뢄석을 μ²˜λ¦¬ν•˜λŠ” 예 :

{
    "plugins": [
        [ "module-resolver", {
            "cwd": "babelrc",
            "alias": {
                "CompanyName.Products": "./path/to/typescript/build/output/CompanyName.Products/index.js"
            }
        }],
        ... other plugins ...
    ]
}

λ‹΅λ³€

이 λ„€μž„ 슀페이슀 λͺ¨λ“ˆμ„ μ‚¬μš©ν•΄λ³΄μ‹­μ‹œμ˜€

namespaceModuleFile.ts

export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}

bookTreeCombine.ts

β€” 컴파일 λΆ€λΆ„ β€”

import {Bookname , TreeList} from './namespaceModule';
import b = require('./namespaceModule');
let BooknameLists = new Bookname.Adventure('Pirate treasure');
BooknameLists = new Bookname.Snows('ways to write a book');
const TreeLis = new TreeList.MangoTree('trees present in nature');
const TreeLists = new TreeList.GuvavaTree('trees are the celebraties');