Building my JS library, a couple of questions

I am building a library (https://github.com/OscarGodson/storageLocker), more accurate local packaging, but since this is my first attempt to use OO JavaScript code, I am still involved and I have a couple of questions.

  • I have seen in other libraries that sometimes wrap them with an anonymous function. Should I do this with this? And if so, how not to break anything?

  • For an internal API (mostly internal functions), how should I write them? Add them to the main object, for example. storageLocker.prototype.myInternalFunction()or just by myInternalFunction()accident in my script? I did not want the functions to be global, though ... One of the functions, for example, just checks a bunch of elements in JSON, sees if their objects are, and then checks the type of the object (for example Date()) and then converts it.

  • How / where should I add global, to script, vars? for example I have var called patternssomething like var patterns = {"date":/\/Date\(([0-9]+)\)\//}how should I add this to my script?

Many thanks. I want to write my script the right way so I ask you guys. I don’t know any JS guy at the local level that any OO JS does, they are all old school types ...

+3
source share
2 answers

http://nefariousdesigns.co.uk/archive/2010/10/object-oriented-javascript-follow-up-part-2-technical/

has a decent section on the value of names.

http://yuiblog.com/blog/2007/06/12/module-pattern/

- Another good review.

For more good material on good javascript practices, check out

http://javascript.crockford.com/

After discussing in the comments, I changed the example:

var storageLocker = function (selector) {

    var _selector = selector || "default value";

    function myPrivateFunction() {

    }

    var public = {

        get: function () {
            return _selector;
        },

        uppercase : function () {
            _selector = _selector.toUpperCase()
            return this;
        }

    }
    return public;

};

// use:
var test = storageLocker("search for this").uppercase().get();;

alert(test);

/ ( , ), , . storageLocker, public, storageLocker .

, , storageLocker, .

+2

:

1) . .   , MyLibrary. API - .

var MyLibrary = function() {
// private
   this.InternalVariable = 'some value';

   function internalFunction(x,y) {
      return x + y;
      }

     function getInternalVariable() {
    return this.InternalVariable;
}
// public
   return {
      publicVariable : '1.0',
      publicFunction : function(x,y) {
         return x + y
         },
 accessInternalVariable : function() {

     return getInternalVariable();
     }
      }
   }();

2) . , ""

3) , setter/getter "private"

+3

Source: https://habr.com/ru/post/1777358/


All Articles