-5

I want to create a globally-accessible library myLib in Javascript. My library should do the following

  • Keep a somewhat persistent variable myLib.tax.

    -- It should survive through the handling of a typical browser user event, at least in simple cases. I don't care if it survives longer than that or not.

    -- external code should be able to set the value of this variable, either through a setter or otherwise.

  • Has a function myLib.applyTax(var price).

    -- return value is price + myLib.tax.

    -- I don't care what it does if either price or tax is not a number.

  • Lastly, a brief example of how to call this from another file, in such a way that:

    -- for example, file A might set the value of the tax, then file B might apply the.

    -- this works even though files A and B are unrelated.

The purpose of this question is for me to understand code and state encapsulation and how to use them from elsewhere.

EDIT: For the benefit of anyone seeing this later, what I did not understand when asking this question is that referring from javascript file A.js to javascript file B.js is a difficult problem unless that is enabled externally, for example in the html. For more detail, see How do I include a JavaScript file in another JavaScript file?

Community
  • 1
  • 1
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • Can include stacksnippets at Question of `js` tried ? – guest271314 Feb 12 '16 at 15:27
  • I'm not sure what you mean. My problem is not with how to add things in js. It's in how to set code up to retain state and be called from elsewhere. – William Jockusch Feb 12 '16 at 15:29
  • What have you tried ? Can you create a minimal verifiable complete example at jsfiddle http://jsfiddle.net or plnkr http://plnkr.co ? – guest271314 Feb 12 '16 at 15:30
  • For example, in C# I would create a static class and everything would be a snap. In js I have no idea where to begin, or even if it is possible. – William Jockusch Feb 12 '16 at 15:30
  • The problem is that I simply have no idea where to begin. In C# I would start off with public static class MyLib and take it from there. But does js even have anything like that? The rest I'm sure will be easy. – William Jockusch Feb 12 '16 at 15:31
  • 1
    This is *definitely* too broad for StackOverflow... – Stephan Bijzitter Feb 12 '16 at 15:32
  • Possible duplicate of [How do JavaScript closures work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Liam Feb 12 '16 at 15:32
  • @Liam No, it's not. This is OP asking how to use one javascript file in any other file. It has very little to do with closures. – Stephan Bijzitter Feb 12 '16 at 15:33
  • @WilliamJockusch It should be possible to create a global object using `myLib = {} /* create an object */; myLib.tax = 123 /* set property tax of myLib */` – guest271314 Feb 12 '16 at 15:34

1 Answers1

2

From your comments I see you would implement a Singleton Pattern in C# through static methods and data.

In Javascript you can do the same by managing the closures and putting that data into the outermost one, which would then be accessible to all code due to lexical scoping.

But I'd rather avoid singletons every day. Instead, use dependency injection as follows. This is just an example to show the technique, you'll have to code an actual solution yourself.

var lex = (function () {
  var privatedata;

  function privatemethod()
  {
  }

  return {
    publicmethod1: function (arg1, arg2) { ... },
    publicmethod2: function (arg1, arg2) { ... },
    getPublicData: function () { ... }
  };
}());

This incapsulated object is then injected wherever you need it:

function consumerCode(lexicon)
{
  ...
}

consumerCode(lex); // injects the lex instance into the consumer code

By having only one instance and passing that same instance around wherever you need it, you will have pretty much what you were asking for. This technique is not limited Javascript but also useful in C#. But in Javascript with loose typing it is especially powerful.

pid
  • 11,472
  • 6
  • 34
  • 63