Organizing my JavaScript function library - Stack Overflow

Over time, I created a bunch of various JavaScript functions.Most of them are static functions, and d

Over time, I created a bunch of various JavaScript functions. Most of them are static functions, and do things like modify the appears of a date, create the select menu HTML from an array, etc.

I currently have them all in a file called "general.js" which is in turn directly called by my HTML page, and each of them look something like:

function modifyDate(data){....}
function makeArray(arr){....}

And then I use them as:

alert(modifyDate("12/14/2013"));

I am thinking this is a bad idea as it might conflict with other libraries. Instead, I am thinking of something like the following:

myLibrary={};
myLibrary.modifyDate= function(data){....}
myLibrary.makeArray= function(arr){....}

And them use them as:

alert(myLibrary.modifyDate("12/14/2013"));

Note that I am kind of making this up as I go. Please provide advice how I should best organize my JavaScript library. Thank you

Over time, I created a bunch of various JavaScript functions. Most of them are static functions, and do things like modify the appears of a date, create the select menu HTML from an array, etc.

I currently have them all in a file called "general.js" which is in turn directly called by my HTML page, and each of them look something like:

function modifyDate(data){....}
function makeArray(arr){....}

And then I use them as:

alert(modifyDate("12/14/2013"));

I am thinking this is a bad idea as it might conflict with other libraries. Instead, I am thinking of something like the following:

myLibrary={};
myLibrary.modifyDate= function(data){....}
myLibrary.makeArray= function(arr){....}

And them use them as:

alert(myLibrary.modifyDate("12/14/2013"));

Note that I am kind of making this up as I go. Please provide advice how I should best organize my JavaScript library. Thank you

Share Improve this question asked Apr 1, 2013 at 16:19 user1032531user1032531 26.4k75 gold badges245 silver badges416 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

What you're describing is called namespacing and is generally considered a good idea.

Some more discussion of namespacing can be found in this question: Why do they use namespaces in javascript?

In general the benefits of namespacing are:

  1. Limiting pollution of the global scope and preventing naming collisions
  2. Providing context for your function names (we'd expect different results for WindowUtils.getHeight and MenuUtils.getHeight for instance).

So your example would provide the first benefit, though not necessarily the second one if this is just a group of grab-bag functions. Whether thats a good thing or not is totally dependent on your individual project and what you're trying to do.

Note that if you're going to be namespacing, you may want to look into the module pattern which is a way of protecting the scope of your namespaces to allow for private variables/protected state. There's a good example in this answer to a similar question or you could check out this canonical blog post on the module pattern

In your case the module pattern would look something like this:

var myLibrary=(function(){
  var privateVariable; //accessible by your functions but not in the global context    
  return {
    modifyDate: function(data){....},
    myLibrarymakeArray: function(arr){....}
  };
}())

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744306810a4567753.html

相关推荐

  • Organizing my JavaScript function library - Stack Overflow

    Over time, I created a bunch of various JavaScript functions.Most of them are static functions, and d

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信