开发者

Creating new JS library - Help deciding the base structure

Im creating a new library for a company. The structure I follow is

    (function() {
            var lib = function() {
                    //some private and public fn definitions

                    //setting publically avbl functions
                    return {
                    func1 : func1, func2: func2
                    };
            };
            return (window.lib = lib);
    })();

Now how I call this is

    lib.func1();

I want to be able to call my library as

    lib(function | string | object).someproperty

How do I convert my code. Tried something like this

    function lib() {
            return new arguments.callee(arguments);
    }

    lib.prototype={
            publicfunc: functi开发者_Go百科on() {
            }
    }

In this i'm having some problems accessing private functions since it is out of the scope for the public functions defined in the lib's prototype.


var lib = (function (param) {

   var func = function () {
     /// your code

     return {
         animate : function () {
          // do the animation
          return this;
     }
   }

   return func;
})();

this can be the basic fprmat.

ok here is how i can be used.

lib(function() {...});

or

lib(selectItem).animate();

because i returned this i can run another method if there is any.

lib(selectItem).animate().animate().animate().animate() ....;

i hope you can find a use of this format, of course i made it very basic


In this i'm having some problems accessing private functions since it is out of the scope for the public functions defined in the lib's prototype.

This is because there is no such thing as private there is only local. You cannot access local variables out of scope.

The prototype cannot talk to local variables in the constructor. There are various hacks around this like keeping a public hash of instances but then you lose the "privacy"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜