哪个js函数更符合工业标准?
我有两个 JavaScript 程序,一个是我在原来的公司使用过的,另一个是我刚刚在网站上遇到的。今天,我注意到我的同事在程序中使用了这两种标准,在最终确定产品之前,我们必须将所有内容都更改为一种标准代码。
<script type="text/javascript">
// Reference the textbox
var txt = document.getElementById("<%=txtName.ClientID%>");
// If the textbox was found, call its focus function
if (txt != null)
txt.focus();
</script>
或
<script type="text/javascript">
// Reference the textbox and call its focus function
var txt = $("#txtName");
txt.focus();
</script>
如果您遇到这种情况,您会建议哪一种,为什么?
I have two JavaScript programs, one I have used in my old company and another one I just came across in a site. Today, I noticed that my colleagues have used both these standards in the program and before finalizing the product, we have to change everything to one standard code.
<script type="text/javascript">
// Reference the textbox
var txt = document.getElementById("<%=txtName.ClientID%>");
// If the textbox was found, call its focus function
if (txt != null)
txt.focus();
</script>
or
<script type="text/javascript">
// Reference the textbox and call its focus function
var txt = $("#txtName");
txt.focus();
</script>
If you are in such a situation, which one will you suggest and why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一个是纯 JS,第二个使用 jQuery 包装。如果您采用 jQuery 作为 JS 框架,那么第二个选项更好:)。
在这种特殊情况下,第二个更好,因为您不必关心测试该变量是否为空(您正在调用 jQuery 元素的焦点函数,因此它将负责检查该元素是否在 DOM 上)或不)
First one is plain JS, second one uses jQuery wrapping. If you have adopted jQuery as JS framework, the second option is better :).
2nd in this particular case is better because you don't have to care about testing if that variable is null or not (you are calling the focus function of a jQuery element, so it will take care about checking whether the element is on the DOM or not)
如果您在团队中工作,您的同事可能会更清楚
txtName.ClientID
的含义。每天都要仔细阅读同事写的许多行代码,我会这么做。或者两全其美,使用类似:
$("#<%=txtName.ClientID%>").focus()
If you are working in a team
txtName.ClientID
could have a meaning that's more clear to your colleagues. Having to plough through many lines of code from my colleagues on a daily base, I would go for that.Or take the best of both worlds and use something like:
$("#<%=txtName.ClientID%>").focus()
第二个使用 jquery,因此它依赖于该库,但它的可读性很强。您甚至可以将其写得更短:
$("#txtName").focus();
第一个更快,但从大局来看,这种差异并不重要,但更容易使用 jQuery 编写和读取代码。
The second one uses jquery, so it is dependant on that libary, but it is much readable. You could even write it shorter:
$("#txtName").focus();
The first one is faster, but in the big picture this difference won't matter much, but it is easier to write and read code with jQuery.
正如 stecb 所说,第一个是纯 JavaScript 代码,第二个是某种 JS 库(最有可能是 jQuery)。这里没有正确或错误的方法,尽管如果您正在加载它,则在整个项目中使用该库是有意义的,而您显然正在加载它。对于现代系统上的基本使用而言,使用纯 JavaScript 带来的潜在性能提升可以忽略不计。
您应该检查是否所有网址/页面都加载了 JS 库,有时开发人员只在某些页面上加载某些库,这会阻止您使用该库,除非您包含它。
As stecb said, the first one is plain JavaScript code, and the second is some sort of JS library (most likely jQuery). There is no right or wrong approach here, even though it would make sense to use the library across the whole project if you are loading it, which you obviously are. Potential performance gains from using plain JavaScript are negligible for basic usage on modern systems.
You should check if all urls / pages load the JS library, sometimes developers only load certain libraries on some pages, which would prevent you from using the library unless you included it.