目录[隐藏]
|
var iterations = Math.ceil(values.length / 8); var startAt = values.length % 8; var i = 0; do { switch(startAt){ case 0: process(values[i++]); case 7: process(values[i++]); case 6: process(values[i++]); case 5: process(values[i++]); case 4: process(values[i++]); case 3: process(values[i++]); case 2: process(values[i++]); case 1: process(values[i++]); } startAt = 0; } while (--iterations > 0);
利用正则表达式去掉前面的,再用charAt、substring忽略尾部的:
function trim(text){ text = text.replace(/^\s+/, ""); for (var i = text.length - 1; i >= 0; i--) { if (/\S/.test(text.charAt(i))) { text = text.substring(0, i + 1); break; } } return text; }
The culprits fall into two main categories: web proxies and PC security software.(哦,原来如此!)
Event delegation is the name commonly given to the technique of attaching a single event handler to a parent element that contains all of the elements that need to respond to the event. When the event is triggered on the child element, it bubbles up to the parent where it is handled. That single handler can distinguish which child element is the target of the event and receive additional parameters via some attribute on that element.
// Wasteful var foo = $("foo"); foo.style.left = "0"; foo.style.right = "0"; foo.style.height = "10px"; foo.style.width = "10px";
// Better var foo = $("foo").style; foo.left = "0"; foo.right = "0"; foo.height = "10px"; foo.width = "10px";
I’ve seen developers spend hours debugging why flushing wasn’t working, only to realize they were connected to the Internet through a company proxy that breaks flushing.
In addition to the traditional definition of universal selectors, Hyatt lumps adjacent sibling selectors, child selectors, descendant selectors, and attribute selectors into this category of “universal rules.” He recommends using ID, class, and tag selectors exclusively.
Because there is only one element in the page with a given ID, there’s no need to add additional qualifiers. For example, DIV #toc is unnecessary and should be simplified to #toc.
Instead of qualifying class selectors for specific tags, extend the class name to be specific to the use case. For example, change LI .chapter to .li-chapter, or better yet, .list-chapter.
Don’t be tempted to build long selectors, such as OL LI A. It’s better to create a class, such as .list-anchor, and add it to the appropriate elements.
Descendant selectors are typically the most expensive to process. Child selectors are often what’s intended and can be more efficient. It’s even better to follow the next guideline to avoid child selectors as well.
If you have a child selector that is based on a tag, such as #toc > LI > A, use a class associated with each of those tag elements, such as .toc-anchor. Question all usages of the child selector This is another reminder to review all places where child selectors are used, and replace them with specific classes when possible.
Learn which properties are inherited, and avoid rules that specify these inherited styles. For example, specify list-style-image on the list element instead of on each list item element. Consult the list of inherited properties to know which properties are inherited for which elements.
Here are the original 13 rules that are still the basis for YSlow’s performance analysis: