Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

296
Views
Explicar el espacio de nombres de TypeScript compilado (funcionamiento interno del espacio de nombres)

Estoy tratando de entender cómo se puede implementar el espacio de nombres en JavaScript. Estoy revisando ¿Cómo declaro un espacio de nombres en JavaScript? .

Una de las respuestas mencionó el siguiente código TypeScript.

 namespace Stack { export const hello = () => console.log('hi') } Stack.hello()

está compilado en el siguiente JavaScript

 var Stack; (function (Stack) { Stack.hello = () => console.log('hi'); })(Stack || (Stack = {}));

Sin embargo, me resulta difícil entender la sintaxis anterior, ¿alguien podría explicar cómo funciona? Por ejemplo, ¿cómo funciona lo siguiente?

 (function(Stack) {...})(...) (Stack || (Stack = {}))
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

 var Stack; (function (Stack) { Stack.hello = () => console.log('hi'); })(Stack || (Stack = {})); Stack.hello();

Es una función anónima autoejecutable. Puede consultar este documento para obtener algunas ideas.

Para una versión más comprensiva, puedes imaginarlo como a continuación

 var Stack; //naming the function //`Stack` here is not related to the global `Stack` because the function parameter scopes this variable function anonymousFunction(Stack) { Stack.hello = () => console.log('hi'); } //replace for `Stack || (Stack = {})` if(!Stack) { Stack = {} //it's a reference value, so if we have any changes inside a function, it will be applied to `Stack` as well } //execute the function/namespace to add `hello` to `Stack` anonymousFunction(Stack); Stack.hello();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!