在本地机器上用 Javascript 轻松开发的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3613952/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:44:07  来源:igfitidea点击:

Best ways to develop painlessly in Javascript on a local machine

javascriptunit-testingtesting

提问by Alex Baranosky

I'm pretty new to workign with Javascript.

我对使用 Javascript 还很陌生。

In most languages you can run the code quickly locally on your machine. From what I've seen, in JS you generally only use it via the browser, and so I've been uploading my code an viewing its effects in the browser. This has proven very tiresome. Also, if I mak one error, it seems like my JS/JQuery will just do NOTHING, instead of giving me a useful error, message, which is making it painfully slow to code in.

在大多数语言中,您可以在本地机器上快速运行代码。据我所知,在 JS 中你通常只能通过浏览器使用它,所以我一直在上传我的代码并在浏览器中查看它的效果。事实证明,这非常令人厌烦。此外,如果我犯了一个错误,我的 JS/JQuery 似乎什么也不做,而不是给我一个有用的错误消息,这使得编码变得非常缓慢。

IS there some way to run JS locally to see that it is working as I go? And then only upload it to the web when I'm mostly done? What ways are there for me to do this? What ways aer there for me to unit test the Javascript locally? Say I have some JAMLthat should render as <p>HI</p>, how do I run this locally in a unit test?

有什么方法可以在本地运行 JS 以查看它是否正常工作?然后只在我大部分完成后才将其上传到网络?我有什么方法可以做到这一点?我有什么方法可以在本地对 Javascript 进行单元测试?假设我有一些JAML应该呈现为<p>HI</p>,我如何在单元测试中本地运行它?

Thanks for the help, Alex

感谢您的帮助,亚历克斯

EDIT:

编辑:

Thanks for all the great suggestions. I'll have to take a bit of time and go through them to see which ones best help me in my situation.

感谢所有伟大的建议。我将不得不花一些时间浏览它们,看看哪些对我的情况最有帮助。

回答by Dani Cricco

You don't need to upload the JS file to a server to test it. Just write an html and declare the js binding

您无需将 JS 文件上传到服务器进行测试。只写一个html并声明js绑定

<script
            src="js/yourJSFile.js"
            type="text/javascript"></script>

Edit the JS file in your favorite editor and then refresh the page to test it.

在您喜欢的编辑器中编辑 JS 文件,然后刷新页面进行测试。

For unit testing the best option is Selenium. It allows you to record an interaction with the browser and then play it back.

对于单元测试,最好的选择是Selenium。它允许您记录与浏览器的交互,然后回放。

回答by Aatch

Since you're using jQuery, I assume that you actually want to manipulate the various elements on your page. So depending on your specific development enviroment, uploading it each time is probably the way to go anyway. If you can set up a dev enviroment on your local machine (not always possible) then go with that.

由于您使用的是 jQuery,我假设您实际上想要操作页面上的各种元素。因此,根据您的特定开发环境,每次都上传它可能是无论如何都要走的路。如果您可以在本地机器上设置开发环境(并非总是可行),那么就使用它。

As an actual answer to your question, I suggest using Chrome's developer tools, it doesn't just have the console, but an element inspector, and a resource tracker (resource tracker is invaluable when working with JSON and AJAX, since invalid json will fail silently)

作为您问题的实际答案,我建议使用 Chrome 的开发人员工具,它不仅有控制台,还有元素检查器和资源跟踪器(资源跟踪器在使用 JSON 和 AJAX 时非常宝贵,因为无效的 json 会失败默默)

As far as I know, the firebug plugin for firefox (dont use it myself) has a similar feature set, so if you're more comfortable with that go with it.

据我所知,firefox 的 firebug 插件(不要自己使用)有一个类似的功能集,所以如果你更喜欢它,那就去吧。

Just remember, as a developer, your development (and debuggin) enviroment is just as important as the code that you are writing.

请记住,作为开发人员,您的开发(和调试)环境与您正在编写的代码一样重要。

EDIT: Noticed that you mentioned unit testing. There are several unit testing frameworks out there for JS including one that integrates with firebug called FireUnit. Do a quick google search to find more if you want.

编辑:注意到你提到了单元测试。有几种用于 JS 的单元测试框架,包括一个与 Firebug 集成的称为FireUnit 的框架。如果需要,请进行快速谷歌搜索以找到更多信息。

回答by Grokys

You can use Firebugwith Firefox to debug JS, and Google Chrome has a debugger built-in (use the Tools -> Developer Tools menu).

你可以使用Firebug和 Firefox 来调试 JS,谷歌浏览器有一个内置的调试器(使用工具 -> 开发者工具菜单)。

回答by Robert

You can run Javascript from the local file on your machine in your browser, so you can skip the uploading step.

您可以在浏览器中从机器上的本地文件运行 Javascript,因此您可以跳过上传步骤。

Also, I'd recommend using Firefox/Firebugcombo for developing Javascript as it will be very handy, especially for the part you mentioned about not seeing what's going wrong with your code.

另外,我建议使用 Firefox/ Firebug组合来开发 Javascript,因为它非常方便,特别是对于您提到的关于看不到代码出了什么问题的部分。

回答by Iznogood

Even if you upload your javascript it gets downloaded back to you as soon as you visit the webpage that invoques it. Its run client side always. So stick to local and use firebug as the others have said. Google`s developer tool is quite nice too.

即使您上传了 javascript,它也会在您访问调用它的网页时立即下载回给您。它始终运行客户端。所以坚持本地并像其他人所说的那样使用萤火虫。Google 的开发者工具也很不错。

回答by Rahul Arora

In the browser if you open the developer tools, follow the following steps:

如果在浏览器中打开开发者工具,请按照以下步骤操作:

1) Navigate to sources

1) 导航到来源

2) Under sources, click snippet and open run.js

2)在sources下,点击snippet并打开run.js

3) You can use run.js to write as much code as you want and run it locally only to see if your code is working or not (it will give you output on the console)

3) 您可以使用 run.js 编写尽可能多的代码,并在本地运行它仅查看您的代码是否正常工作(它会在控制台上为您提供输出)

4) Also you can get used to some keyboard shortcuts to make it faster for you.

4) 你也可以习惯一些键盘快捷键,让你更快。

5) For small javascript codes, you can navigate to console and run your code there

5)对于小的javascript代码,您可以导航到控制台并在那里运行您的代码

回答by HoLyVieR

If you want to do unit testing with Javascript there are extension of Firebug that can help you with that. I haven't try any of them, so I can't really tell you which one are worth considering, but you can easily find them if you search for the keyword "Firebug unit testing"on Google.

如果你想用 Javascript 进行单元测试,Firebug 的扩展可以帮助你。我没有尝试过它们中的任何一个,所以我不能真正告诉你哪个值得考虑,但是如果你在谷歌上搜索关键字“Firebug 单元测试”,你可以很容易地找到它们。

What seems to be comming on top is FireUnit. You can find some information about how it works here.

似乎排在首位的是FireUnit。您可以在此处找到有关其工作原理的一些信息。

回答by tchen

Consider Spider Monkey, which is a javascript engine separate from a browser. If what you are developing does not involve rendering to a webpage or can be separated from the rendering code (good practice!), then this could be useful.

考虑Spider Monkey,它是一个独立于浏览器的 javascript 引擎。如果您正在开发的内容不涉及渲染到网页或可以与渲染代码分离(好的做法!),那么这可能很有用。

回答by Nate May

I prefer Chrome to Firefox and I just found Web Server for Chrome.

我更喜欢 Chrome 而不是 Firefox,我刚刚找到了Web Server for Chrome

It's just a Google App that quickly sets up a web server for you and will be set up anywhere you are logged into Chrome. It only allows file access to your current devices, or if you specify, other devices only on the current LAN.

它只是一个 Google 应用程序,可以为您快速设置网络服务器,并且可以在您登录 Chrome 的任何地方进行设置。它只允许文件访问您当前的设备,或者如果您指定,则只允许当前 LAN 上的其他设备。

You just point it to the directory with your index.html file and type http://127.0.0.1:8887in your browser.

您只需将其指向 index.html 文件所在的目录,然后http://127.0.0.1:8887在浏览器中输入即可。

回答by sebisnow

Additionally to the answers given you can use Jasmine for automated testing.

除了给出的答案,您还可以使用Jasmine进行自动化测试。


A tutorial that seems to help get started with automated testing on Jasmine is provided by Evan Hahn.
I used it and for me it works like a charm. Especially if test driven development is what you are going for!


Evan Hahn 提供了 一个似乎有助于开始在 Jasmine 上进行自动化测试的教程
我用过它,对我来说它就像一个魅力。特别是如果测试驱动开发是您的目标!