What do you think?
Rate this book
370 pages, Paperback
First published January 1, 2008
on every XYZ,
___________________________________________________________
#01: page 105
___________________________________________________________
// ---------------------
// as illustrated:
Function.prototype.partial = function() {
var fn = this, args = Array.prototype.slice.call(arguments);
return function() {
var arg = 0;
for (var i = 0; i < args.length && arg < arguments.length; i++) {
if (args[i] === undefined) {
args[i] = arguments[arg++];
}
}
return fn.apply(this, args);
};
};
// ---------------------
/*
observation:
============
args is modified within the closure.
the 1st invokation will replace all undefined values.
all subsequent invokations will use the same identical parameters as specified by the 1st invokation.
*/
// ---------------------
// test:
var test_group = function(){
var delay = setTimeout.partial(undefined, 10);
delay(function(){
console.log('1st invokation..');
});
delay(function(){
console.log('2nd invokation..');
});
delay(function(){
console.log('3rd invokation..');
});
}
test_group();
// ---------------------
/*
console output:
===============
1st invokation..
1st invokation..
1st invokation..
*/
// ---------------------
// fix:
Function.prototype.partial = function() {
var fn = this, args_tpl = Array.prototype.slice.call(arguments);
return function() {
var args = Array.prototype.slice.call(args_tpl);
var arg = 0;
for (var i = 0; i < args.length && arg < arguments.length; i++) {
if (args[i] === undefined) {
args[i] = arguments[arg++];
}
}
return fn.apply(this, args);
};
};
// ---------------------
// test:
test_group();
// ---------------------
/*
console output:
===============
1st invokation..
2nd invokation..
3rd invokation..
*/
___________________________________________________________
// #02: page 246
___________________________________________________________
// ---------------------
/*
code:
=====
function getAllElements(name) {
if (!window.findByTagWorksAsExpected) {
window.findByTagWorksAsExpected = (function(){return boolean;})();
}
if (!window.findByTagWorksAsExpected) {
// do stuff..
}
}
*/
// ---------------------
/*
observation:
============
since the purpose of the 1st if() statement is to ensure that the value is only calculated once,
this methodology will fail when the calculated value === false.
the code should read something closer to..
*/
// ---------------------
/*
alt. code:
==========
function getAllElements(name) {
if (typeof window.findByTagWorksAsExpected !== 'boolean') {
window.findByTagWorksAsExpected = (function(){return boolean;})();
}
if (!window.findByTagWorksAsExpected) {
// do stuff..
}
}
*/
___________________________________________________________
// #03: page 246
___________________________________________________________
// ---------------------
/*
observation:
============
the statement:
if (allElements[n].nodeType === 1)
should be:
if (allElements[n].nodeType !== 1)
*/
// ---------------------
// test:
// browser = Firefox 24.0
// url = www.google.com
var allElements;
allElements = document.getElementsByTagName('*');
allElements = Array.prototype.slice.call(allElements);
console.log(allElements.length);
for (var n = allElements.length - 1; n >= 0; n--) {
if (allElements[n].nodeType === 1){
allElements.splice(n,1);
}
}
console.log(allElements.length);
// ---------------------
/*
console output:
===============
306
0
*/
// ---------------------
/*
summary:
========
I removed the outer conditional:
if (!window.findByTagWorksAsExpected){ ... }
By doing so, I can illustrate what would happen in IE when this loop runs.
Rather than removing "comment" nodes, it removes "element" nodes;
thereby returning an array of only "comment" nodes.
In FF, where the NodeList contains 0 "comment" nodes..
the final array will always have a length of 0.
*/
___________________________________________________________
// #04: page 259
___________________________________________________________
// ---------------------
/*
observation:
============
the statement:
var begin = new Date();
should occur after all other such variable initialization;
only the 1st test includes the call to:
getElementById()
which could possibly skew the results.
*/
___________________________________________________________
// #05: page 317-318
___________________________________________________________
// ---------------------
/*
thinking out loud:
==================
- triggerSubmitOnXYZ()
* bubble the "submit" event beginning from "this" (ie: not e.target)
* checks that e.target is contained within a