Writing a Javascript library that is suitable for code completion and code verification

I recently created my own Javascript library, and first used the following template:

var myLibrary = (function () {

  var someProp = "...";

  function someFunc() {
    ...
  }

  function someFunc2() {
    ...
  }

  return {
     func: someFunc,
     fun2: someFunc2,
     prop: someProp;
  }

}());

The problem is that I cannot really use code completion because the IDE is not aware of the properties returned by the function literal (by the way, I am using IntelliJ IDEA 9).

I looked at jQuery code and tried to do this:

(function(window, undefined) {
    var myLibrary = (function () {

      var someProp = "...";

      function someFunc() {
        ...
      }

      function someFunc2() {
        ...
      }

      return {
         func: someFunc,
         fun2: someFunc2,
         prop: someProp;
      }

    }());

    window.myLibrary = myLibrary;
}(window));

I tried this, but now I have a different problem. The IDE is not really dialing myLibrary.

Now I solve the problem as follows:

var myLibrary = {
   func: function() { },
   func2: function() { },
   prop: ""
};

myLibrary = (function () {

  var someProp = "...";

  function someFunc() {
    ...
  }

  function someFunc2() {
    ...
  }

  return {
     func: someFunc,
     fun2: someFunc2,
     prop: someProp;
  }

}());

But that seems pretty awkward, and I can't figure out exactly how jQuery does it. Another question I have is how to handle functions with an arbitrary number of parameters.

, jQuery.bind 2 3 , IDE, , . , 0 1 . IDE , . ?

, Idea9, jQuery . , , .

+3
5

IDEA yahoo, . Google yahoo.

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

http://ajaxian.com/archives/a-javascript-module-pattern


TEST = function() {
    var SOME_CONSTANT='asd';

    function privateStuff(){
        var a = 'asd';
        return a;
    }

    return{
        someArray:[],

        someMethod: function(foo, bar){

            var foo = *1
        }
        ,

        myProperty:'test'
    }
}();

TEST.*2

* 1 * 2 , .

in * 1 SOME_CONSTANT privateStuff, (), return {} block

* 2, return {} block. SOME_CONSTANT privateStuff , "private".

.

+3

, , - . yahou YUI. , . 2 . 1.-

{ option :{ var1 : "value" , var2:"value"}, var3 : "value" }

, .

, undefined.

function foo(var1,var2){
   var var1_this = null;
     if(var1 != undefined)
      var1_this = var1;
}

, javascript? Prototype, JQuery, Mootols, YUI. ?

+1

mwilcox.

. myLibrary var, . , , - myLibrary. , Firebug Rhino.

, .. Prototypal _:

// Pseudoclassical pattern
function Hello() {}
Hello.prototype = {
    method1: function() {},
    method2: function() {},
    _pseudeoPrivate: function() {}
};

/* Prototypal pattern. To create multiple instances of this object,
   you need a helper function such as
    function beget(o) {
        var F = function() {};
        F.prototype = o;
        return new F;
    }
    var instance = beget(world);
*/
var world = {
    method1: function() {},
    method2: function() {}
};

, , public api . jQuery. , ( intro.js outro.js) Github.

, IDE ( ctags with vim) api, .

+1

, , , intellisense. :

(function(){
    var privateVar = "shhhh!";
    var privateMethod = function(){}


    myLibray = {
        prop:42,
        foo: function(){
            return privateMethod()
        },
        bar: function(){
            return privateVar;
        }
    }

})();

, , .

[. myLibrary , . . ]

, : http://clubajax.org/javascript-private-variables-are-evil/

0

:

function MyLibrary() {
    // code
}
MyLibrary.prototype.memberFunc = function() {
    // code
}
MyLibrary.prototype.memberVar = 5;

new MyLibrary();

, Geany ( CTAGS) MyLibrary ( , , memberVar ), , , . IDEA9, ( , , CTAGS).

0

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


All Articles