How to install underscore.js?

I am trying to install underscore.js , so I can use it in my browser, but it seems that all the installation instructions are for servers, How can I use this in my web browser? I know JS does not have imports or requires , so I'm not sure what to do. Thanks

+4
source share
5 answers

Please indicate which browser you are using, but little comes to mind:

  • Go to JSFiddle or JSBin or other alternatives, enable or select the JS infrastructure that you want to use and play with it.
  • JS . HTML-, JS-.

    • Firefox, addon Firebug, , , SO google.com

      var script = document.createElement("script"); script.src = "http://path/to/underscor.js"; document.body.appendChild(script);

      JS .

    • Google Chrome, F-12, "", " " , , , JS . . -, , JS . , Firefox. .
  • , Browserify.

, - HTML- JS-. IMHO, , JSFiddle, JS- . HTML , script .

+2
  • - Google Chrome Mozilla Firefox. , google.com.
  • F12.
  • "". :

var script= document.createElement('script');     script.type = 'text/javascript';     script.src= 'https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js';     document.head.appendChild(script);

Enter.

js .

, , .

+6

JavaScript - . , (, first underscore.js, , underscore.js). - (CDN), . CDN:

, ( , ).

, underscore.js, , :

JS library demo.js

// function using underscore.js
function demo() {
    var input = [1, 2, 3];
    var output = _.map(input, function(item) {
            return item * 2;
    });
    alert(input + " - " + output);
}

demo.html:

<!DOCTYPE HTML>
<html>
    <head>
        <!-- first include the underscore.js library -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore.js" type="text/javascript"></script>
        <!-- or if the file is downloaded locally -->
        <!-- <script src="scripts/underscore.js" type="text/javascript"></script>-->
        <!-- then the custom JS library -->
        <script src="demo.js" type="text/javascript"></script>
    </head>
    <body>
        <!-- call the custom library function -->
        <a href="#" onclick="demo();">Start the script using underscore.js</a>
    </body>
</html>

, :

1,2,3 - 2,4,6
+3

Just paste the following code into the head section of your .html file.

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3  /underscore-min.js">
</script>
+2
source

You can simply load it with a tag <script>. Looking at the code, it will load into the global object ( window == this).

  var root = this;

  ...

  if (typeof exports !== 'undefined') {
    if (typeof module !== 'undefined' && module.exports) {
      exports = module.exports = _;
    }
    exports._ = _;
  } else {
    root._ = _;
  }
0
source

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


All Articles