-
Notifications
You must be signed in to change notification settings - Fork 2
从阅读JQuery源码中发现的18个惊奇的地方
I love jQuery, and though I consider myself an advanced JavaScript developer, I had never read the jQuery source from top to bottom, until now. Here are a few things that I learned along the way: 我喜欢JQuery,虽然我自认为自己试JS的高级开发人员,但是之前一直没有从头到尾的度过它的源码。最近有机会读了它的源码,并记录下来我学到的东东。
Note: I use the
Sizzle’s weight: Sizzle is the selector engine jQuery uses to find elements in a DOM based on CSS selectors. It’s what turns
$.grep: This method is similar to Underscore’s .filter in that it takes two arguments, an array of elements and a function, and returns the elements that pass the function’s truth test. $.grep:这个方法类似于Underscore的.filter。它有两个参数,分别是数据集合和回调方法。返回值是过滤后的集合。
Bubbling caveats: jQuery specifically prohibits one type of event from ever bubbling. That is the load event. Internally, jQuery passes a special noBubble: true flag through with any load events, so that image.load events can’t bubble up to the window (which could mistakenly match a window.load event). 事件冒泡的注意事项:jQuery中load事件是不能进行冒泡的。事实上,jQuery传递了一个参数noBubble为true的值给了load事件。因此image.load事件不会冒泡到window(这个可能会和window.load事件搞混淆了)
Default animation speed: jQuery animates elements by changing their style attributes in quick succession. Each of these changes is called a “tick.” The default animation speed is to run a tick every 13 milliseconds, and you can adjust this by overriding jQuery.fx.interval with your own integer. 默认的动画渲染频率:jQuery动画元素是通过快速改变其样式属性而形成的。每一个这样的变化称为“帧”。默认的动画速度是每帧13毫秒,你可以重写jQuery.fx.interval来调整渲染频率。
So does
:empty pseudo selector: This convenient pseudo selector will match elements with no children. :empty伪选择器:这个方便的选择器不能匹配子节点(译者注:也不能匹配文本节点)
:lt and :gt pseudo selectors: These pseudo selectors match elements based on their index in the matched set. For example,
$.fn.clone arguments: When you .clone() an element, you can also clone its data attributes and events by passing true as the first argument to clone.
More $.fn.clone arguments: In addition to the above, you can clone its children’s data attributes and events by passing an additional true argument. This is called a "deep clone." That second argument defaults to the value of the first (whose default is false). So if the first is true and you want the second to be true, you can omit the second argument entirely.