[英]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: 最具体地说,我正在寻找以下问题的答案:
gui.scrollbar.attach()
, or a global method such as $(domObj).attachScrollbar()
? 最好使用命名空间(例如gui.scrollbar.attach()
还是全局方法(例如$(domObj).attachScrollbar()
? 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, 进一步来说,
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: 关于您的一些问题:
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.