我了解一些 Javascript,但刚刚意识到我对跨浏览器问题知之甚少。令人讨厌的小事情,例如 IE 中事件回调中的 this
对象(如 xhr.onreadystatechange = function () { ... }
)不引用函数所在的对象应用于,而不是应用于 window
,这并不完全有用。
SO 上有一个令人印象深刻且全面的差异列表。
是否还有一个库可以涵盖这些令人讨厌的跨浏览器问题,而不向您出售整个生活方式以及带有幻灯片效果的圆角?我知道 jQuery 很棒(而且是模块化的,我知道 UI 是额外的;我敢打赌其他的也很棒),但我正在寻找更精简、更接近根源的东西。只是做最少的事情并消除肮脏的事情。不必用糖包裹 DOM。
更新
感谢大家的建议。我将了解一下 MyLib、microJS、Ender 和 Sizzle。我认为,GWT 虽然确实是跨浏览器的,但并不是一种轻量级方法,但绝对是一种有趣的方法。
I know some Javascript, but just realized I know very little about cross-browser issues. Nasty little things like the this
object in event callbacks in IE (as in xhr.onreadystatechange = function () { ... }
) not referring to the object the function is applied to, but instead to window
, which is not exactly useful.
There's an impressive and comprehensive-looking list of differences here on SO.
Is there also a library that covers these nasty cross-browser issues without selling you a whole lifestyle plus round corners with slide effects? I know jQuery is great (and modular, I know, UI coming as an extra; and I bet others are great, too), but I'm looking for something lean, closer to the roots. Just doing the minimum and eliminating the nastiness. Doesn't have to wrap the DOM in sugar.
Update
Thanks everybody for your suggestions. I'm going to take a look at MyLib, microJS, Ender, and Sizzle. GWT, while certainly being cross-browser, is not, I think, a lightweight approach, but definitely an interesting one.
发布评论
评论(6)
jQuery 不是模块化的——要么全有要么全无。如果您想要一个可靠的跨浏览器库,并且可以将其修剪到所需的最低限度,那么很难超越 MyLibrary :http://www.cinsoft.net/mylib.html。
“MyLibrary”这个名称意味着当您下载并自定义它时,它就成为您的库。
它绝对坚固、快速且极其模块化。您可以只使用您想要的部分并删除任何不必要的部分。
顺便说一句,许多像 jQuery 这样的库并不是真正的“跨浏览器”,它们是多浏览器的——它们支持的浏览器集有限,并且不太关心其余的浏览器。另一方面,MyLibrary 被编写为真正的跨浏览器。它还提供了出色的功能检测快捷方式,因此您可以轻松编写带有回退功能的健壮代码。
jQuery is not modular - it's all or nothing. If you want a solid, cross browser library that you can trim to the minimum you require, it's hard to go past MyLibrary: http://www.cinsoft.net/mylib.html.
The name "MyLibrary" means that when you download and customise it, it becomes your library.
It is absolutely solid, fast and extremely modular. You can use just the bits you want and remove anything unnecessary.
BTW, many libraries like jQuery aren't really "cross browser", they are multi–browser — they have a limited set of browsers that they support and don't care much about the rest. On the other hand, MyLibrary is written to be genuinely cross–browser. It also provides excellent feature detection shortcuts so you can easily write robust code with fallback.
你想要什么?
只需检查 microJS 并下载您想要的库。
正如已经提到的,你可以使用 Ender 来捆绑它们
What do you want?
Just check microJS and download the libraries you want.
As mentioned already you can use Ender to bundle them
我立即想到了 MyLib。
您甚至可以使用此在线工具构建您自己的自定义版本。
I immediately thought of MyLib.
You can even build your own custom version using this online tool.
我认为你应该看看 Ender 作者:达斯蒂安·迪亚兹和雅各布·桑顿在推特。
I think you should have a look at Ender By Dustian Diaz and Jacob Thornton working at Twitter.
JavaScript 中
this
的问题在于,对于那些不习惯它总是获取上下文值这一事实的人来说,它可能有点令人困惑,换句话说,它总是指向一个对象。位于执行代码的当前上下文中。在某些事件、间隔等情况下,this 指向 window 是绝对正常的,因为 JavaScript 中的很多(可能太多)属性都附加到 window 对象。
至于在你的工作中使用哪个 JS 库...好吧,如果你不想整体使用 jQuery,那么它总是有最重要的部分,它处理 DOM 内对象的选择,并且对于跨浏览器非常重要兼容性。
它称为Sizzle,可以在此处找到。它不像 jQuery 那样提供花哨的东西,但它很小,并且提供了一种很好的跨浏览器方式来选择页面上的东西。
Well the problem with
this
in JavaScript is that it can be a bit confusing to people which are not accustomed to the fact that it always gets a contextual value or in other words it will always point to the object which is in the current context of the executing code.In case of some events, intervals etc. its absolutely normal that this points to window because a LOT (perhaps too much) of properties in JavaScript are attached to the window object.
As for which JS library to use for your work...Well if you don't want to use jQuery as a whole there is always the most important part of it which handles selection of objects inside DOM and is pretty much important for cross browser compatibility.
Its called Sizzle and can be found here. It doesn't offer fancy stuff like jQuery does but it is small and offers a great cross-browser way to select stuff on pages.
您可以查看 GWT...但它确实也向您出售一种生活方式 - Java 开发环境。但这也带来了调试器、合适的 IDE、更简单的 OO、它编译为优化的跨浏览器 JavaScript 等。并且您始终可以在您认为合适的地方混合和匹配本机 JavaScript。
You can look at GWT.. but it does sell you a lifestyle as well - a Java dev environment. But that also brings in a debugger, a proper IDE, easier OO, it compiles to optimized cross-browser javascript etc. And you can always mix and match native JavaScript where you see fit.