编写 javascript 小部件的最佳实践 [英] Best practices for writing javascript widgets

查看:24
本文介绍了编写 javascript 小部件的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个添加到其他网站的 JS 脚本(小部件).从技术上讲,它类似于谷歌分析.我的问题是:对于确保稳健性、防止代码干扰网站上的其他代码、避免用户出错等,您有什么建议.总的来说,我应该知道什么才能编写专业级的小部件.

I have a JS script (widget) which is added to other websites. Technically its similar to Google analytics. My question is: what are your advices for ensuring robustness, keeping the code from interfering with other code on the site, avoiding errors for users etc. In general, what should I know to write a professional grade widget.

注意事项:我不能使用任何 JS 库,例如 jquery 等.

Notes: I can't use any JS library such as jquery etc..

推荐答案

我非常喜欢 Peter Michaux 关于如何他编写 javascript 小部件

I am a big fan of Peter Michaux's guide on how he writes javascript widgets

Christian Heilmann 的脚本配置也很有用和 模块模式

Also useful are Christian Heilmann's script configuration and the module pattern

那些是通用的 javascript 文章,并不特定于单个库

Those are generic javascript articles and aren't specific to a single library

其他有用的技巧包括将代码包装在匿名函数中以阻止它干扰其他全局库.

Other useful tricks are things like wrapping your code in an anonymous function to stop it interfering with other global libraries.

(function() {
    //Your code goes in here
})();

关于错误和最佳实践,John Resig 有一篇关于 javascript strict 还没有,但确实有一些关于您应该避免的事情的方便信息.

Regarding errors and best practice, John Resig has an interesting article on javascript strict that isn't in yet, but does have some handy information on the sort of things you should be avoiding.

如果您仍然对对象内的范围界定感到困惑,那么您可能会找到这篇文章 关于私有和公共变量的有用以及由 Douglas Crockford

If you're still coming to terms with scoping within your objects, then you might find this article on private and public variables useful as well a a bit more technical definition by Douglas Crockford

最后,记得通过代码质量工具

这篇关于编写 javascript 小部件的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆