javascript 我如何安装 underscore.js?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24232725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I install underscore.js?
提问by chopper draw lion4
I am trying to install underscore.jsso I can use it in my browser, but it seems all installation instructions are meant for servers. How do I use this in my web browser? I know JS has no import or requireso I am not sure what to do. Thanks
我正在尝试安装underscore.js以便我可以在浏览器中使用它,但似乎所有安装说明都是针对服务器的。我如何在我的网络浏览器中使用它?我知道JS 没有导入或要求,所以我不知道该怎么做。谢谢
采纳答案by Harsh Gupta
Please include what browser you are using, but few things come to mind:
请包括您使用的浏览器,但很少想到:
- Head over to JSFiddleor JSBinor other alternatives, include or select the JS framework you want to use and play with it.
Using JS in a browser means nothing. There's got to be some HTML code involved that could use and understand JS code.
Firefox
, install addon likeFirebug
, open a simple page like one of SO or google.com and in the consolevar script = document.createElement("script"); script.src = "http://path/to/underscor.js"; document.body.appendChild(script);
Then you could start using functions in your JS file.
Google Chrome
, click F-12, go to Sources tab, click on Content Scripts in left panel, right click to add folder containing your JS files. That should work as well. There is also another sub-tab called snippets in left panel, create a new file and just copy paste entire JS file into it. Alternatively, you could follow the same technique for Firefox. Its Developer Panel is much more advanced and sophisticated.
You can try and look at things like Browserify.
- 转到JSFiddle或JSBin或其他替代方案,包括或选择您想要使用的 JS 框架并使用它。
在浏览器中使用 JS 毫无意义。必须涉及一些可以使用和理解 JS 代码的 HTML 代码。
Firefox
,安装插件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,转到 Sources 选项卡,单击左侧面板中的 Content Scripts,右键单击以添加包含 JS 文件的文件夹。这也应该有效。左侧面板中还有另一个名为片段的子选项卡,创建一个新文件并将整个 JS 文件复制粘贴到其中。或者,您可以对 Firefox 遵循相同的技术。它的开发人员面板更加先进和复杂。
您可以尝试查看 Browserify 之类的东西。
The gist is, you need some kind of HTML to invoke and use JS code. IMHO, tools like JSFiddle are much better at using and testing some JS code and involves less hassle. Or just create a simple HTML file on your system, include a script tag and test it.
要点是,您需要某种 HTML 来调用和使用 JS 代码。恕我直言,像 JSFiddle 这样的工具在使用和测试一些 JS 代码方面要好得多,而且麻烦更少。或者只是在您的系统上创建一个简单的 HTML 文件,包括一个脚本标记并对其进行测试。
HTH
HTH
回答by Gil Baggio
- Open some webpage in Google chrome or Mozilla Firefox. For example, google.com.
- And then press F12 key.
- Select Console Tab.And the type or Copy-paste the following code:
- 在 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);
var script = document.createElement('script'); script.type = '文本/javascript'; script.src = ' https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js'; document.head.appendChild(script);
and press Enter.
并按 Enter。
Then start typing your underscore js commands on the console.
然后开始在控制台上输入下划线 js 命令。
回答by pasty
You don't installa JavaScript library in order to use it - you need to includeit. If you have dependencies, then only the order (for example first underscore.js and then your custom library that uses underscore.js) is important. One possibility would be to use some Content Delivery Network (CDN), so you don't need to download the library locally. Common CDN'sare:
您不需要安装JavaScript 库来使用它——您需要包含它。如果您有依赖项,那么只有顺序(例如,首先是 underscore.js,然后是使用 underscore.js 的自定义库)是重要的。一种可能性是使用一些内容交付网络 (CDN),因此您无需在本地下载库。常见的CDN有:
If you download the library and host it on your server, than just put it in your project directory (or in a directory called scripts).
如果您下载该库并将其托管在您的服务器上,那么只需将其放在您的项目目录(或名为scripts的目录中)。
The code that includes the underscore.jslibrary used from a custom library could look like this:
包含自定义库中使用的underscore.js库的代码可能如下所示:
JS library demo.js
JS 库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);
}
and then in a second file demo.html:
然后在第二个文件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>
The output is as expected:
输出符合预期:
1,2,3 - 2,4,6
回答by Nelliusz Fr?cek
Just paste following code into head section of your .html file.
只需将以下代码粘贴到 .html 文件的 head 部分即可。
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3 /underscore-min.js">
</script>
回答by joeytwiddle
You should just be able to load it using a <script>
tag. Looking at the code shows it will load itself into the global object (window == this
).
您应该能够使用<script>
标签加载它。查看代码表明它将自己加载到全局对象 ( window == this
) 中。
var root = this;
...
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}