Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  130] [ 4]  / answers: 1 / hits: 18093  / 14 Years ago, mon, may 17, 2010, 12:00:00

I have a general question about the design of JavaScript Libraries.



I am trying to consolidate common methods into one js file so they can be reused by different scripts.



I have taken a look at how the JSON library is structured and believe it was a good approach.
JSON for Javascript.



So they start off creating an instance of the class:



if (!this.JSON) {
this.JSON = {};
}


Then they do this:



(function () {
if (typeof JSON.stringify !== 'function') {
JSON.stringify = function (value, replacer, space) {


This is works perfect if you just want to do JSON.[function_name], but what if I want to have a more structured library such that I want: JSON.[subgroup].[function]. How would I structure my JS library in such a way?



Any links to resources are greatly appreciated, thanks.


More From » javascript

 Answers
31

I would recommend you to follow the Module Pattern in JavaScript instead of following JSON's pattern strictly. Your subgroup is actually referring to sub-modules. Take a look at the following excellent article:



Ben Cherry's JavaScript Module Pattern In-Depth



Other resources you should probably go through:




[#96769] Thursday, May 13, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
taylert

Total Points: 627
Total Questions: 91
Total Answers: 108

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
taylert questions
;