JavaScript Patterns 2.3 - For loops

HTMLCollections are objects returned by DOM
methods such as:

? document.getElementsByName()

? document.getElementsByClassName()

? document.getElementsByTagName()

HTMLCollections, which were introduced before the
DOM standard and are still in use today

document.images

All IMG elements on
the page

document.links

All A elements

document.forms

All forms

document.forms[0].elements

All fields in the
first form on the page

// used i+=1 instead of i++ to follow the rule of
JSLint

for (var i = 0, max = myarray.length; i < max; i += 1) {

// do something with myarray[i]

? Use one less variable (no max)

? Count down to 0, which is usually faster
because it‘s more efficient to compare to 0 than to the length of the array or
to anything other than 0

The first modified pattern is:


var i, myarray = [];

for (i = myarray.length; i--;) {

// do something with myarray[i]

}

And the second uses a whileloop:


var myarray = [],
i = myarray.length;

while (i--) {

// do something with myarray[i]

}

JavaScript Patterns 2.3 - For loops,布布扣,bubuko.com

时间: 2024-08-03 16:26:52

JavaScript Patterns 2.3 - For loops的相关文章

JavaScript Patterns 2.5 (Not) Augmenting Build-in Prototypes

Disadvantage Other developers using your code will probably expect the built-in JavaScript methods to work consistently and will not expect your additions. Properties you add to the prototype may show up in loops that don't use hasOwnProperty(), so t

JavaScript Patterns 5.4 Module Pattern

MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var uobj = MYAPP.utilities.object, ulang = MYAPP.utilities.lang, // private properties array_string = "[object Array]", ops = Object.prototype.toStr

JavaScript Patterns 3.7 Primitive Wrappers

Primitive value types: number, string, boolean, null, and undefined. // a primitive number var n = 100; console.log(typeof n); // "number" // a Number object var nobj = new Number(100); console.log(typeof nobj); // "object"  One reason

JavaScript Patterns 2 Essentials - Writing Maintainable Code

Revisiting the code after some time has passed requires: ? Time to relearn and understand the problem ? Time to understand the code that is supposed to solve the problem As the application matures, many other things happen that require your code to b

JavaScript Patterns 7.1 Singleton

7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was created the first time. var obj =

JavaScript Patterns 2.12 Writing API Docs

Free and open source tools for doc generation: the JSDoc Toolkit (http://code.google.com/p/jsdoc-toolkit/) and YUIDoc (http://yuilibrary.com/projects/yuidoc). Process of generating API documentation ? Writing specially formatted code blocks ? Running

JavaScript Patterns 2.11 Writing Comments

Document all functions, their arguments and return values, and also any interesting or unusual algorithm or technique. Think of the comments as hints to the future readers of the code; the readers need to understand what your code does without readin

JavaScript Patterns 2.2 - Minimizing Globals

Access a global variable in a browser environment: myglobal = "hello"; // antipattern console.log(myglobal); // "hello" console.log(window.myglobal); // "hello" console.log(window["myglobal"]); // "hello"

JavaScript Patterns 4.7 Init-Time Branching

When you know that a certain condition will not change throughout the life of the program, it makes sense to test the condition only once. Browser sniffing (or feature detection) is a typical example. // BEFORE var utils = { addListener : function(el