0

I'm writing a library and I'm not sure how to keep things secret (I don't want my constructors to be available as global objects).

// This is a no-no.
var SecretObj = function() { ... }
SecretLib.prototype.SecretObj = SecretObj;

But of course, I can do this :

// This is a no-no.
SecretLib.prototype.SecretObj = function() { ... }

// Its even worse when I have more 
// prototypes on the SecretObj. Ex : 
SecretLib.prototype.SecretObj.prototype.wowitslong = function() { ... }

But then again it's not good enough, so I did this :

// For more short code.
var SecretLib.fn = SecretLib.prototype;

(function(){

  var SecretObj = function() { ... }
  SecretObj.fn = SecretObj.prototype;
  
  // And then
  SecretObj.fn.finally = function() { ... }
  
  // And  to finish
  SecretLib.fn.SecretObj = SecretObj;

})()

// But then, it becomes quite messy to write : 
SecretLib.SecretObj.finally

// Or something like
SecretLib.SecretObj.InnerSecretObj.omg();

I don't know if it's good enough or if there is a better way. Thanks for your help.

Bird
  • 572
  • 5
  • 15
  • 1
    Define "secret". – Ram Aug 29 '17 at 01:38
  • Not in the window object. Well nowhere except in SecretLib. Can it be more secret? ^_^ Oh, I see it's true, I can also make "private constructors" that are returned by a function. But that method is maybe to overkill. I just need a simple way to attach it to SecretLib's prototype without declaring the object globally. But if there is a good practice, I am ready to hear it. – Bird Aug 29 '17 at 01:40
  • 1
    Well, why don't you want your "constructors to be available as global objects"? Having global constructors is not a sin! If you define your constructor in a self-invoking function context then you can only use it in that context! If you want to code in a more modular manner you can use [AMD](http://requirejs.org/docs/whyamd.html), browserify (CommonJS) or ... – Ram Aug 29 '17 at 01:51
  • When I think about libraries, I think about a package deal ...so I guess it must come in one beautiful object? I don't know haha, I'm trying to make things clean, but not to clean ( I will never finish if I do that :D). By the way, thanks for the link! By the way, I updated my last script (I forgot something).. – Bird Aug 29 '17 at 02:01
  • 1
    Using a self-invoking function for creating a local scope is the right of doing this! I'd suggest reading [this article](http://ifandelse.com/its-not-hard-making-your-library-support-amd-and-commonjs/) which shows how you can make your package ready to be used in different environments. – Ram Aug 29 '17 at 02:19
  • 1
    And [another article](http://krasimirtsonev.com/blog/article/javascript-library-starter-using-webpack-es6) that uses webpack for building the package (more ceremony!) – Ram Aug 29 '17 at 02:27
  • Haha, thanks. You can post an answer and I'll accept it later tomorrow. It will give me the time to read the articles and maybe get other suggestions. Thanks a lot! – Bird Aug 29 '17 at 02:34
  • A secret library makes no sense, as that implies its existence is a secret even to those who want to use it. You also need to export something to somewhere - either the global scope or some other kind of static module registry. – Bergi Aug 29 '17 at 02:53
  • It sounds like you're looking for the [module pattern](https://stackoverflow.com/questions/26092101/what-is-this-javascript-pattern-called-and-why-is-it-used). – Bergi Aug 29 '17 at 02:53
  • Ok guys, look here : https://stackoverflow.com/questions/46026438/amd-building-library-example. Thanks for your awesome links!! I love it. I think I'm going to delete this question after some time as it was answered in the comments. Well You could also post an answer. :). – Bird Sep 03 '17 at 18:15

0 Answers0