JavaScript API 开发的良好实践
设计 JavaScript API 的好方法是什么?
我对 JavaScript 比较陌生,并且主要从“JavaScript:优秀部分”学习该语言的主要优秀功能。目前,我正在设计一个基于网络的工具来教授统计学。代码库变得越来越笨重,部分原因是我不明白如何设计 JavaScript API。
我的背景是 Java 和 C++,习惯于设计接口然后独立实现这些接口。显然,这在 JavaScript 中效果不佳。
感谢您的任何帮助和建议。
更新:该工具的最终版本位于:http://www.lock5stat.com/statkey/index.html lock5stat.com/statkey/index.html
What's a good approach to designing a JavaScript API?
I'm relatively new to JavaScript and learning the key good features of the language, mostly from "JavaScript: The Good Parts". Currently, I'm designing a web based tool to teach Statistics. The code base is getting unwieldly in part because I don't understand how to design a JavaScript API.
My background is in Java and C++ and am used to designing interfaces then implementing those interfaces independently. Obviously, this doesn't work well in JavaScript.
Thanks for any help and suggestions.
Update: Final version of the tool here: http://www.lock5stat.com/statkey/index.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,如果您使用 jQuery,最好的解决方案是将您的应用程序分为 jQuery 插件和 UI 小部件。插件是组织代码的好方法,您可以在其他应用程序中重用它们,也可以单独发布它们。
您可以将应用程序编写为插件树。您可以考虑像 C++/Java 中的类那样的插件,具有聚合但不具有继承。
关于代码的注释:在 MAIN.bootstrapPlot 函数中,您使用 document.createElement,您应该像代码的其余部分一样使用 jQuery,并检查 Canvas 的 Canto 库。
In my opinion, if you're using jQuery, the best solution is to divide your application into jQuery plugins and UI widgets. Plugins are great way to organize the code, you can reuse them in other applications and you can also release them separately.
You can write your applications as a tree of plugins. You can think about plugins like classes in C++/Java with aggregation but not inheritance.
Notes about code: in MAIN.bootstrapPlot function you use document.createElement, you should use jQuery like in the rest of the code, and check Canto library for Canvas.
John Resig 对 NodeIterator API 提出了严厉的批评。尽管这是对一个特定的评论API,它提供了一些关于如何成为一个好工具的见解(至少他认为是这样)。
John Resig has a scathing critique of the NodeIterator API. Although it's a review of one specific API, it provides some insights into (at least what he believes) makes for a good tool.
我认为这与开发任何 API 一样,无论使用何种语言,都要牢记语言特性。
正如您指出的,接口在 javascript 中没有多大意义,但它是面向对象的,因此您可以将功能划分为“类”,使用设计模式等。
文档。您可以像记录任何 API 一样记录 javascript“类”。
即使 js 中没有对接口或抽象类的本机支持,您仍然可以强制执行这些原则。例如,您可以通过定义一个带有抛出错误的方法的对象来创建一个“抽象类”,这样子类如果没有正确实现,就会出错。任何使用接口的东西都可以做一个简单的检查,看看 init 上是否有必要的方法。请注意,一些 js 人会对此皱眉,指出 js 是松散类型和动态的,应该以这种方式使用。
请注意,我将“class”放在引号中,因为 javascript 没有这样的概念。您可以做的另一件事是查看现有的 API(例如 Sencha 或 jQuery)来获取一些想法。 Sencha 感觉就像服务器端开发人员会习惯的 API(查看他们的文档)。
I think its the same as developing any API regardless of language, keeping in mind the language features.
As you point out interfaces don't make much sense in javascript, but it is object oriented, so you can divide your functionality into 'classes', use design patterns, etc.
Documentation. You can document a javascript 'class' just like you document any API.
Even though there is no native support for interfaces or abstract classes in js, you can still enforce the principles. For example, you can create an 'abstract class' by defining an object with methods that throw errors, that way subclasses will error out if they are not implemented properly. Anything that uses an interface can do a simple check to see if the necessary methods are in place on init. Note that some js people will frown upon this, pointing out that js is loosely typed and dynamic, and should be used that way.
Note that i put 'class' in quotes because javascript has no notion as such. The other thing you can do is look to existing APIs like Sencha or jQuery to get some ideas. Sencha feels like an API a server side developer would be used to (look at their docs).