将旧的 javascript 转换为 jQuery 的工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11341932/
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
Tool to translate old javascript into jQuery
提问by Clément Andraud
I know there are lots of tools on the net that can make our lives easier, for us, developers, including some that are quite powerful.
我知道网络上有很多工具可以让我们的生活更轻松,对于我们开发人员来说,包括一些非常强大的工具。
I ask if you know a tool to translate old javascript into jQuery?
我问你是否知道将旧的 javascript 翻译成 jQuery 的工具?
A tool that can help make this stuff ? Because I have to translate thousands of lines of code in jQuery, and I think I have about 15 years... :)
一个可以帮助制作这些东西的工具?因为我必须在 jQuery 中翻译数千行代码,而且我想我有大约 15 年的时间...... :)
Thanks !
谢谢 !
回答by ThiefMaster
No, such a tool doesn't exist. If it existed the code created by it wouldn't be something anyone wanted to work with.
不,这样的工具不存在。如果它存在,那么它创建的代码就不会是任何人想要使用的东西。
The best way to start using jQuery is simply using jQuery for new stuff and if there's time slowly migrating old stuff manually - preferably stuff that's broken or needs modifications anyway.
开始使用 jQuery 的最佳方法是简单地将 jQuery 用于新的东西,如果有时间慢慢地手动迁移旧的东西 - 最好是已经损坏或需要修改的东西。
回答by rjmunro
The question doesn't make sense. jQuery is not a language that you can translate into. jQuery is a library that you can use in your Javascript code if you want. There is nothing jQuery can do that can't be done without it.
这个问题没有意义。jQuery 不是一种您可以翻译成的语言。jQuery 是一个库,您可以根据需要在 Javascript 代码中使用它。没有 jQuery 没有它做不到的事情。
What you probably want is a tool to help you refactor your Javascript code, replacing specific patterns with equivalent jQuery methods. The problem is that this would produce a mess.
您可能需要一个工具来帮助您重构 Javascript 代码,用等效的 jQuery 方法替换特定模式。问题是这会造成混乱。
E.g. the jQuery equivalent to:
例如jQuery相当于:
var x = document.getElementById('foo');
is:
是:
var x = $('#foo');
but now x is a jQuery object, not a DOM object, so the code that uses it will break.
但是现在 x 是一个 jQuery 对象,而不是一个 DOM 对象,所以使用它的代码会中断。
You could do:
你可以这样做:
var x = $('#foo')[0];
which would give you a DOM object, but then you are wasting jQuery.
这会给你一个 DOM 对象,但是你在浪费 jQuery。
One solution is to replace the code with:
一种解决方案是将代码替换为:
var $x = $('#foo');
var x = $x[0];
Then stick to the convention that $var
is the jQuery wrapped version of var
. As refactoring progresses, you can use a tool that tells you 'x' is unused (like jsLint) to know that it's safe to remove it.
然后坚持$var
使用 jQuery 包装版本的约定var
。随着重构的进行,您可以使用一个告诉您“x”未使用的工具(如 jsLint)来知道可以安全地删除它。
Various IDEs have tools to refactor Javascript a bit. See this question for some: How do you refactor JavaScript, HTML, CSS, etc?
各种 IDE 都有一些工具可以稍微重构 Javascript。请参阅以下问题:如何重构 JavaScript、HTML、CSS 等?