使用 JQuery 而不是直接使用 JavaScript 可以节省多少行代码?

发布于 2024-08-02 13:53:23 字数 208 浏览 2 评论 0原文

也许更熟悉该主题的人可以帮助我简化或正确写出问题。我们正在寻找 JQuery 甚至其他 JS 框架与直接 JavaScript 的使用进行比较的指标。我不相信代码行度量,从来没有,但我无法想到其他任何东西来衡量它。

Function Points (jquery) = Jquery-LOC
Function Points (JS) = JS-LOC

Maybe someone more familiar with the topic can help me to simplify or write the question correctly. We are looking for metrics on JQuery or even other JS Frameworks in comparison with the use of straight javascript. I don't beleive in the lines of code metric, never have, but I cannot think on anything else to measure it.

Function Points (jquery) = Jquery-LOC
Function Points (JS) = JS-LOC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

天涯离梦残月幽梦 2024-08-09 13:53:23

要使用标准 javascript 附加点击处理程序,您可以尝试如下操作:

var myelement = document.getElementById("clickme");
function myClickHandler (evt) {
  var evt = evt || event; // ie uses a global instead of
                          // passing an object as a param
  (evt.target || evt.srcElement).style.width="110px";

}
if (myelement.addEventListener) {
   myelement.addEventListener("click", myClickHandler, false);
} else if(myelement.attachEvent) {
   myelement.attachEvent("onclick", myClickHandler);
} else if(typeof myelement.onclick === "function") {
    (function () {
      var oldfunc = myelement.onclick; 
      myelement.onclick = function (evt) {
          var evt = evt || event;
          oldfunc(evt);
          myClickHandler(evt);
      }
    })()
} else {
   myelement.onclick = myClickHandler;
}

或尝试使用 jquery 执行类似的操作

jQuery("#clickme").click(function(evt){ jQuery(evt.target).css("width",110); });

您可能会说,每次要分配点击处理程序时编写第一个示例代码是一件愚蠢的事情。为什么不将其重构为自己的标准函数并重用呢?那么你就有了一个可以调试的核心函数,而无需在发现错误时重写每个实例。

这将是一个非常好的论据,而这正是 jquery 这样的库的意义所在。通过不编写这个冗长的样板文件可以节省时间,如果您希望代码跨浏览器工作,那么这是绝对必要的。无需调试即可节省时间。您可以节省时间 - 如果您确实需要调试它,您只需修补 jquery 中的一个函数并为下一版本提交错误报告。如果事件绑定代码有问题,那么只有一个人需要更改一点代码,而不是成千上万的人需要更改数百万位代码。当有人指出你应该首先测试 addEventListener 时,这一点就变得毫无意义,因为 jQuery 已经做了既定的“正确”事情。如果没有,它将在下一个版本中出现。

jQuery 不会帮助你解决真正困难的领域特定问题,但为了磨练日常的 dom 操作,最好使用库函数,而不是试图“接近金属”。有太多跨浏览器的碎片,还有太多愚蠢冗长的 dom API,而且一遍又一遍地解决已经解决的问题太愚蠢了。

你需要某种类型的库(不一定是 jquery)才能保持理智。

现在您到底节省了多少时间和精力?谁知道?这是无法估量的。您必须绑定事件处理程序多少次?您需要在一个简单函数中执行 jQuery 提供的其他操作多少次?一个人强烈依赖另一个人。

如果您找到了衡量程序员生产力的方法,请告诉我们。我想我们都会非常感兴趣。无论如何,从这些客观事实和我们个人的主观经验来看,我们知道这可能是相当长的时间。

To attach a click handler using standard javascript, you might try something like this:

var myelement = document.getElementById("clickme");
function myClickHandler (evt) {
  var evt = evt || event; // ie uses a global instead of
                          // passing an object as a param
  (evt.target || evt.srcElement).style.width="110px";

}
if (myelement.addEventListener) {
   myelement.addEventListener("click", myClickHandler, false);
} else if(myelement.attachEvent) {
   myelement.attachEvent("onclick", myClickHandler);
} else if(typeof myelement.onclick === "function") {
    (function () {
      var oldfunc = myelement.onclick; 
      myelement.onclick = function (evt) {
          var evt = evt || event;
          oldfunc(evt);
          myClickHandler(evt);
      }
    })()
} else {
   myelement.onclick = myClickHandler;
}

or try to do something similar using jquery

jQuery("#clickme").click(function(evt){ jQuery(evt.target).css("width",110); });

You could argue that the first example code is a stupid thing to write every time you want to assign a click handler. Why not refactor that out into its own standard function, and reuse that? then you have a central function you can debug without having to rewrite every single instance once you've found something wrong.

This would be a very good argument, and that's exactly the point of a library like jquery. You save time by not writing this verbose boilerplate that's absolutely necessary if you want your code to work cross browser. You save time by not having to debug it. You save time by- if you do have to debug it, you just have to patch one function in jquery and file a bug report for the next version. If there's something wrong with the event binding code, then it's just one guy that has to change one bit of code, rather than thousands of people that need to change millions of bits of code. And when someone points out that you should test addEventListener first, the point becomes moot, because jQuery already does the established "correct" thing. And if it doesn't, it will in the next version.

jQuery won't help you on the really difficult domain specific problems, but for grinding through every day dom manipulations, it's better to use a library function than trying to wing it "close to the metal". There's too many cross browser splinters sticking out, and too many dumb verbose dom APIS, and it's too stupid to solve problems that have already been solved, over and over again.

You need SOME kind of library (not necessarily jquery) just to stay sane.

Now how much time and effort do you save, exactly? Who knows? it's immeasurable. How many times do you have to bind an event handler? How many times do you have to do any of the other things that jQuery provides in a simple function? One is strongly dependent on the other.

If you ever find a way to measure programmer productivity, do let us know, though. I think we'd all be very interested. In any case, we know from these objective facts, and our own personal subjective experiences that it's probably quite a substantial amount of time.

咆哮 2024-08-09 13:53:23

您对代码行度量的怀疑是正确的,尽管假设您不考虑 jquery 本身,它将节省大量的代码行。

更重要的是,它将减少您必须在 5 个左右的浏览器上手动编写和测试的 DOM-soup 代码量。它可以节省大量时间,并减少大量错误。

You are right to doubt the lines of code metric, although, assuming you discount jquery itself, it will save substantial new lines of code.

FAR more importantly, it will reduce the amount of the DOM-soup code you have to write and test by hand across 5 browsers or so. It is a massive time saver, and a massive bug reducer.

荒人说梦 2024-08-09 13:53:23

这取决于你写的是什么。想要更精确吗?正好 12 行。

It depends on what you're writing. Want more precision? Exactly 12 lines.

明月夜 2024-08-09 13:53:23

“节省时间”指标与其他指标一样好。

使用 jQuery,你可以写得更少(我的意思是这甚至是 jQuery 的座右铭!),但 LOC 是一个荒谬的衡量标准。

我使用 jQuery 的原因是为了浏览器兼容性。如果我不必自己写,显然可以节省时间。

The 'time saved' metric is as good as any.

Using jQuery, you write less, (I mean it's in jQuery's motto even!), but LOC is a ridiculous measurement.

The reason I use jQuery is for the browser compatibility. If I don't have to write that myself, obviously I save time.

厌倦 2024-08-09 13:53:23

开发人员的生产力如何?找到支持代码库的人有多容易。

How about how productive the developer is? How easy is it to find someone to support the code base.

野味少女 2024-08-09 13:53:23

使用 JQuery 或其他 JavaScript 库,您的应用程序将具有更多的功能点和更多的代码行。为什么?因为在开始使用这些库之前,您将可以轻松地完成一些您认为不可能或不可行的事情。这将鼓励您编写更多功能,而不是像从头开始编程那样惩罚您的尝试。

他们就是那么好。

Using JQuery or other JavaScript libraries, your application will have many more function points and have many more lines of code. Why? Because you will be doing things easily that you hadn't thought possible or feasible before you started using these libraries. This will encourage you to write more features, rather than punish you for trying, as programming it from scratch does.

They're that good.

妖妓 2024-08-09 13:53:23

它的数量与一根绳子的长度相当。

It's a comparable amount to that of a length of string.

旧街凉风 2024-08-09 13:53:23

我也不认为 LOC 是一个非常适用的指标,但是能够编写诸如 $('#my_id')document.getElementById('my_id') 之类的东西> 从长远来看,即使在短期内也会产生巨大的影响。

I don't think LOC is a very applicable metric, either, but being able to write things like $('#my_id') vs document.getElementById('my_id') makes an enormous difference in the long run—even in the short run for that matter.

念三年u 2024-08-09 13:53:23

如果没有上下文,这个问题基本上毫无意义。

如果您使用 jQuery 在 4 行中完成一些原本需要 20 行的操作,那么您会因为添加(在版本 1.3.2 上)4376 行 jQuery 来节省 16 行您自己的代码而失败。

另一方面,如果您使用 jQuery 节省了 9000 行代码,那么您就会获得净收益。

如果您需要三个月的时间来学习编写这 16 行所需的内容,那么为了节省 16 行而花费 4376 行甚至可能是值得的。或者,也许值得花三个月的时间来学习足够的知识,以避免写 9000 行,而 20 行就可以了。

我认为你需要一个更复杂的指标。

The question is basically meaningless without context.

If you use jQuery to do something in four lines that would have otherwise have taken 20 lines, you fail by having added (on version 1.3.2) 4376 lines of jQuery to save 16 lines of your own code.

On the other hand, if you save writing 9000 lines of code by using jQuery then you have a net gain.

It may even be that the 4376 lines to save 16 lines are worth it if it would take you three months to learn what's necessary to write those 16 lines. Alternatively, it may be that it's worth taking the three months to learn enough to avoid writing 9000 lines where 20 would do.

I think you need a more complex metric.

谈下烟灰 2024-08-09 13:53:23

我知道这是一个老问题&这不是一个非常重要的问题,但是,让我添加我的OP。看看 Brenton 的例子:

jQuery("#clickme").click(function(evt){ jQuery(evt.target).css("width",110); });

如果 IE8 &下面的内容与上面的内容显然不一样,可以写成:

document.getElementById('clickme').addEventListener('click', function(e){e.target.style.width = '110px';}, false);

在比较 LOC 时,没有人会期望用纯 JavaScript 从头开始​​编写每个应用程序,通过 jQuery(一个通用库)更可行。

在公平的竞争环境中,您可以比较两个应用程序:
App-A 使用 jQuery + libs VS App-B 使用纯 JS + libs。
App-B 可能比 App-A 使用更多的库,这意味着 Js 核心代码比预想的要少。

从整体上看,一个通用库可能大约为 0.?-4kB,并且可以执行以下操作,并且因为它不是一个适合所有库的库,所以包含在 App-B 中是公平的:

getEl('clickme').ev('click',{ width: 110 });

与 JS 相比,jQuery 的行数当然会胜出,但区别在于比较的方法。

I know this is an old question & not a hugely important question but, let me add my OP. Looking at Brenton's example:

jQuery("#clickme").click(function(evt){ jQuery(evt.target).css("width",110); });

If IE8 & below wasn't around the above could obviously be written as:

document.getElementById('clickme').addEventListener('click', function(e){e.target.style.width = '110px';}, false);

When comparing LOC no one is expected to write every application from scratch in pure JavaScript it's more feasible via jQuery, a general purpose lib.

On an even playing field you could compare two apps:
App-A using jQuery + libs VS App-B using pure JS + libs.
App-B is likely to use more libs than App-A which is a lib, meaning less Js core code than envisioned.

To put It all into perspective, a non-general purpose lib maybe around 0.?-4kB and could do the below and because it's not a 1 fits all lib it's fair to include with App-B:

getEl('clickme').ev('click',{ width: 110 });

jQuery will of course wins in terms of a smaller line count vs JS but, the difference is down to the approach of the comparison.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文