Search

5/26/2008

p59
Performance Pitfall
• Array.length is expensive
• better:
• best:
for (var count = 0; count < myarray.length; count++)
console.log(count);
re-calculated
EVERY TIME
--SLOW-- !!
for (var count = 0, mylen = myarray.length; count < mylen; count++)
console.log(count);
for (var count = myarray.length-

p63
try/catch/finally/throw
• Create an exception
• Custom exceptions
try {
43534987yhfh // clearly an error
} catch(myerr) {
console.log(myerr);
} finally {
//code block
}
always executes
regardless of what
happens in catch() or
try()
try {
throw(“User entered invalid data..”);
} catch(myerr) {
console.log(myerr);
} finally {
//code block
}

p107
• Important function methods:
– call(scope, arg1, arg2 …);
– apply(scope, [arg1, arg2 …]);
– caller
• Call and apply used to dynamically
execute a function in arbitrary scope
function showLength() {
alert(this.length);
}
showLength.call(new Array(10)); // Alerts 10!

沒有留言: