Why is Crockford using this format to write the JSON.parse function?

in this function JSON.parse: https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js

Why does crockford choose to declare so many functions in its variable declarations, and then separate 1 function and then return the main function at the end?

Is it possible to write such a function:

pseudo syntax:

var json_parse = (function () {
    var x,
        y, 
        z, 
        string = function () {
             <code>
        },
        word = function () {
             <code>
        },
        white = function () {
             <code>
        },
        value;

    value = function () {
        <code>
        white();
        string();
        etc..
    };
    return function (string) {
         return something;
    }
})();

against writing such a function:

var parse_json = function (input) {
     var x, y, z;

     function string () {
           <code>
     }
     function word () {
           <code>
     }
     function white () {
           <code>
     }
     function value () {
         <code>
          white();
          string();
          etc..
     }

    function mainParse () {
         return something;
    }

   return mainParse(input);

};

Hope my little code examples make sense. I'm new to JavaScript, and I want to make sure I'm learning best practices or writing great features like this.

+4
source share
1 answer

word, white... , json_parse. , , - .

+7

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


All Articles