Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
Structure of pure JavaScript API

I am relatively new to JavaScript, which means i only used jquery, meteor.js and some WebGL (but in no larger project) Since i am very interested in Computer graphics, i want to write a simple WebGL library ( like three.js oder pixie.js but of course more simpler)

The Problem i am facing is for example when i have a namespace MyLibrary.renderer=... i only want to expose MyLibrary. What i am looking for is that a client can only write var renderer =new MyLibrary.renderer()

However inside the file renderer i will need a couple of other classes. The Problem so far is that i import them via <script> but as i mentioned i dont want to expose this classes, but instead only the final namespace MyLibrary. So i was looking for a way to import classes .

I currently looked up browserify but i think i didnt properly understand. Can someone provide also an example of how to use browserify with namespace and classes? And if possible explain how a library like three.js manage/structure their Code?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Write a self executing closure function. This protects your code from mixing up with other library's scope and is generally good practice. Of course it will not prevent others from reading your code.

(function ()
{
  var _MyLibrary = Object.create(null);
  // do stuff
  window.MyLibrary = _MyLibrary;
})();

You can write and expose a constructor function instead of an object as well, however, that's old school. You should prefer generating properties on an object by Object.defineProperties() method.

Instead of loading other codes into <script> tags consider fetching them via AJAX from within your library.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda