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脚本(小部件),它被添加到其他网站。从技术上讲,它类似于Google分析。我的问题是:你有什么建议来确保健壮性,防止代码干扰网站上的其他代码,避免用户的错误等。一般来说,我应该知道如何编写专业级小部件。
Notes: I can't use any JS library such as jquery etc..
注意:我不能使用任何JS库,如jquery等。
1 个解决方案
#1
39
I am a big fan of Peter Michaux's guide on how he writes javascript widgets
我是Peter Michaux关于如何编写javascript小部件的指南的忠实粉丝
Also useful are Christian Heilmann's script configuration and the module pattern
Christian Heilmann的脚本配置和模块模式也很有用
Those are generic javascript articles and aren't specific to a single library
这些是通用的javascript文章,并不是特定于单个库
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
})();
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.
关于错误和最佳实践,John Resig有一篇关于javascript strict的有趣文章还没有,但确实有一些关于你应该避免的事情的方便信息。
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
如果您仍然在对象中使用作用域,那么您可能会发现有关私有和公共变量的文章以及Douglas Crockford的更多技术定义
Finally, remember to run your completed code through a code quality tool
最后,请记住通过代码质量工具运行已完成的代码
#1
39
I am a big fan of Peter Michaux's guide on how he writes javascript widgets
我是Peter Michaux关于如何编写javascript小部件的指南的忠实粉丝
Also useful are Christian Heilmann's script configuration and the module pattern
Christian Heilmann的脚本配置和模块模式也很有用
Those are generic javascript articles and aren't specific to a single library
这些是通用的javascript文章,并不是特定于单个库
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
})();
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.
关于错误和最佳实践,John Resig有一篇关于javascript strict的有趣文章还没有,但确实有一些关于你应该避免的事情的方便信息。
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
如果您仍然在对象中使用作用域,那么您可能会发现有关私有和公共变量的文章以及Douglas Crockford的更多技术定义
Finally, remember to run your completed code through a code quality tool
最后,请记住通过代码质量工具运行已完成的代码