简体   繁体   English

编写可移植Javascript库的最佳方法是什么?

[英]What's the best way to write a portable Javascript library?

All of my projects include very similar tasks and quite recently I've been thinking I should write a library to handle most of the heavy lifting so I can write short, simple, easy to read code that interacts with the library to get the jobs done. 我所有的项目都包含非常相似的任务,最近我一直在思考应该编写一个库来处理大部分繁重的工作,以便我可以编写简短,简单,易读的代码,与该库进行交互以完成工作。 Looking at other frameworks, specifically jQuery & YUI, they work in different ways. 查看其他框架,特别是jQuery和YUI,它们以不同的方式工作。 jQuery mostly seems to extend & simplify the DOM using a global object/function whereas YUI uses namespaces and modules. jQuery似乎主要使用全局对象/函数来扩展和简化DOM,而YUI使用名称空间和模块。 I understand that this is because they have different goals as libraries and each have their merits. 我了解这是因为它们作为库具有不同的目标,并且各有优点。

Most specifically, I'm looking for answers to the following questions: 最具体地说,我正在寻找以下问题的答案:

  1. Is it best to use namespaces, eg gui.scrollbar.attach() , or a global method such as $(domObj).attachScrollbar() ? 最好使用命名空间(例如gui.scrollbar.attach()还是全局方法(例如$(domObj).attachScrollbar()
  2. Is it best to include individual parts as seperate js files or as a single js file where components are copied/pasted? 最好将各个部分作为单独的js文件或作为复制/粘贴组件的单个js文件包括在内? I know the eBay Shopping API SDK has many .js files in seperate folders, wouldn't this suffer a significant performance hit? 我知道eBay Shopping API SDK在单独的文件夹中有许多.js文件,这会不会对性能造成重大影响?
  3. Is there any other advice you would give to someone looking to create a javascript library? 您还有其他建议给想要创建JavaScript库的人吗?

I've marked as community wiki because I didn't want to risk the question being closed - I'm genuinely seeking advice here. 我被标记为社区Wiki,因为我不想冒险被关闭的问题-我是在这里真正地寻求建议。

Thanks 谢谢

EDIT I should have originally stated that I'm not re-inventing the wheel, I'm looking to simplify many common tasks between Windows Desktop Gadgets, so I don't need the cross-browser compatibility of other libraries and their core functionality doesn't really apply to what I'm doing. 编辑我本来应该声明我不是在重新发明轮子,而是希望简化Windows桌面小工具之间的许多常见任务,所以我不需要其他库的跨浏览器兼容性,并且它们的核心功能不需要真的不适用于我正在做的事情。 I tried to keep the question general and not specifically relating to desktop gadgets, so that this question would be useful for others. 我试图使这个问题笼统,而不是专门针对台式机配件,这样这个问题对其他人将很有用。

Simple answer to your question: write your library using an existing one. 您问题的简单答案: 使用现有库编写您的库

I have some javascript files written in jQuery just for such purposes. 我有一些用jQuery编写的javascript文件就是出于这种目的。

More specifically, 进一步来说,

  1. If you are going for java-style object oriented-ness, go for namespaces. 如果您要使用Java风格的面向对象特性,请使用名称空间。 I prefer the jquery way of writing plugins. 我更喜欢用jquery的方式编写插件。
  2. If you are writing your own "toolkit", it would be best to copy and paste the dependencies (minified) into your source code. 如果您正在编写自己的“工具箱”,则最好将依赖项(最小化)复制并粘贴到源代码中。 This way 这条路
    a. 一种。 you avoid the overhead you mentioned 你避免了你提到的开销
    b. you prevent unnecessary dependencies creeping up externally. 您可以防止不必要的依赖从外部蔓延。

Cheers! 干杯!

Concerning your first question, the two options are really the same IMO. 关于您的第一个问题,这两个选项实际上是相同的IMO。 Instead of a global object like YAHOO or gui, you have a global function like $. 而不是像YAHOO或gui这样的全局对象,而是像$这样的全局函数。 In both cases you have a global variable that holds your namespace. 在这两种情况下,您都有一个全局变量来保存您的名称空间。

Is there any reason why you can't use one of the many libraries out there already? 有什么理由为什么您不能使用已有的众多库之一? This seems like a case of reinventing the wheel. 这似乎是重新发明轮子的情况。 These libraries have been around for years and have already tackled many of the issues that you will surely run into when trying to write your own. 这些库已经存在多年了,并且已经解决了您在尝试编写自己的库时必定会遇到的许多问题。

In terms of some of your questions: 关于您的一些问题:

  1. This is really a matter of preference IMO. 这确实是IMO的优先事项。 As long as you get your functions out of the global namespace that is the major thing. 只要您将函数移出全局命名空间,这就是主要的事情。 However, this design choice will drive many others down the line. 但是,这种设计选择会推动许多其他方面的发展。
  2. It is best to include all core functionality in one file and then to chunk the rest into files that are required for a bit of functionality. 最好将所有核心功能包括在一个文件中,然后将其余的功能块化为一些功能所需的文件。 You want to keep the number of files down, but you also don't want the user to have to download a lot of stuff that they don't need. 您想减少文件数量,但也不想让用户下载很多他们不需要的东西。

Having said all that, I restate that you should look at using one of the libraries already out there unless you have a very good reason not to. 说了这么多,我重申您应该考虑使用已经存在的一种库,除非您有很好的理由不这样做。 Keep in mind that you will need to test in all old browsers as well as any new browsers that come out. 请记住,您将需要在所有旧浏览器以及任何新出现的浏览器中进行测试。 jQuery, YUI, Prototype, Mootools, extJS are a few of the most popular jQuery,YUI,Prototype,Mootools,extJS是最受欢迎的几种

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

相关问题 用javascript编写正则表达式的最佳方法是什么? - What is the best way to write a regular expression in javascript? 用JavaScript编写对象方法的最佳方法是什么? - What is the best way to write object methods in JavaScript? 为JavaScript编写此RegEx表达式的最佳方法是什么 - What is the best way to write this RegEx expression for JavaScript 在Windows Mobile设备上编写JavaScript / Ruby应用程序的最佳方法是什么? - What's the best way to write JavaScript/Ruby applications on Windows Mobile device? 编写通过JavaScript而非纯HTML解析的链接的最佳方法是什么? (jQuery选项卡) - What's the best way to write links that resolve by JavaScript rather than pure HTML? (jQuery tabs) 使用 XHTML 和 Javascript 编写符合标准的弹出窗口的最佳方法是什么? - What's the best way to write a standards compliant popup window with XHTML and Javascript? 在JavaScript中计算关键字的最佳方法是什么? - What's the best way to count keywords in JavaScript? 调试Javascript / AngularJS的最佳方法是什么? - What's the best way to debug Javascript/AngularJS? 为 OpenTest 学习 JavaScript 的最佳方法是什么 - What's the best way to learn JavaScript for OpenTest 将 JavaScript 注入 Puppeteer 的最佳方法是什么? - What's the best way to inject JavaScript into Puppeteer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM