简体   繁体   English

我什么时候应该使用javascript框架库?

[英]When should I use a javascript framework library?

I'm writing a small web that just makes some animation and shows some information as a homepage and a list of links. 我正在写一个小网页,只是制作一些动画,并显示一些信息作为主页和链接列表。 All that is going to be generated dynamically in the client side. 所有这些都将在客户端动态生成。 So everything is going to be javascript and XML. 所以一切都将是javascript和XML。

Recently I've been reading some questions in SO about javascript, and most of the situations involved the use and/or recommendation of a framework (jquery and friends). 最近我一直在阅读有关javascript的一些问题,大多数情况涉及框架(jquery和朋友)的使用和/或推荐。 When a small web development should start considering the use of such a framework? 当一个小的Web开发应该开始考虑使用这样的框架?

I've been until now doing my stuff just with plain javascript, as far as I'm not implementing a big site is it worth the learning a framework? 直到现在我一直在用普通的javascript做我的东西,只要我没有实现一个大网站是否值得学习框架?

Thanks 谢谢

I'd start right now. 我现在就开始吧。 Libraries like jQuery and prototype not only insulate you from browser differences, but also provide you with a shorthand for communicating your ideas to other programmers. 像jQuery和prototype这样的库不仅可以使您免受浏览器差异的影响,还可以为您提供将您的想法传达给其他程序员的简写。

On SO you will find a lot of people (including me) who advocate the use of jQuery (in particular). 在SO上你会发现很多人(包括我)主张使用jQuery(特别是)。 To me, it's everything a framework should be: small, lightweight, extensible, compact yet powerful and brief syntax and it solves some pretty major problems. 对我而言,这是框架应该具备的一切:小巧,轻便,可扩展,紧凑但功能强大且语法简洁,它解决了一些非常重要的问题。 I would honestly have a hard time trying to envision a project where I wouldn't use it (or another framework). 老实说,我很难设想一个我不会使用它的项目(或其他框架)。

The reason to use it is to solve browser compatibility issues. 使用它的原因是解决浏览器兼容性问题。 Consider my answer to javascript to get paragraph of selected text in web page : 考虑我对javascript的回答, 以获取网页中所选文本的段落

 function getSelectedParagraphText() { var userSelection; if (window.getSelection) { selection = window.getSelection(); } else if (document.selection) { selection = document.selection.createRange(); } var parent = selection.anchorNode; while (parent != null && parent.localName != "P") { parent = parent.parentNode; } if (parent == null) { return ""; } else { return parent.innerText || parent.textContent; } } 

If you're familiar with Javascript a lot of this should be familiar to you: things like the check for innerText or textContent (Firefox 1.5) and so on. 如果您熟悉Javascript,那么您应该熟悉很多内容:例如检查innerText或textContent(Firefox 1.5)等等。 Pure Javascript is littered with things like this. 纯粹的Javascript充斥着这样的事情。 Now consider the jQuery solution: 现在考虑jQuery解决方案:

function getSelectedParagraphText() {
  var userSelection;
  if (window.getSelection) {
      selection = window.getSelection();
  } else if (document.selection) {
      selection = document.selection.createRange();
  }
  var parent = selection.anchorNode;
  var paras = $(parent).parents("p")
  return paras.length == 0 ? "" : paras.text();
}

Where jQuery really shines though is with AJAX. 尽管使用AJAX,jQuery真正闪耀的地方。 There JavaScript code snippets around to find the correct object to instantiate (XMLHttpRequest or equivalent) to do an AJAX request. 有JavaScript代码片段可以找到要实例化的正确对象(XMLHttpRequest或等效的)来执行AJAX请求。 jQuery takes care of all that for you. jQuery为您处理所有这些。

All of this for under 20k for the core jQuery Javascript file. 所有这一切都在20k以下的核心jQuery Javascript文件。 To me, it's a must-have. 对我来说,这是必须的。

Whenever writing javascript isn't your business . 每当写javascript 不是你的业务

JS libraries, other than providing helpers and shortcuts, also take care of corner cases, browser incompatibilities and quirks, and best practices. 除了提供帮助程序和快捷方式之外,JS库还可以处理角落案例,浏览器不兼容性和怪癖以及最佳实践。 It is better that you spend time developing your application, and fall back to native JS only if you have to. 最好花时间开发应用程序,只有在必要的情况下才能回归原生JS。

The deal with jquery is the approach to do javascript but with less work and communicate easier to others, so I would say Yes 与jquery的交易是做javascript的方法,但工作量少,沟通更容易,所以我会说是的

Think of it this way, would you rather write a paper in microsoft word or notepad 想一想,你宁愿在微软的单词或记事本中写一篇论文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM