JavaScript Tips
aymanh.com
aymanh.com
if (!window.console) {
window.console = {
log:function(){},
debug:function(){},
trace:function(){}
};
} if ( errror ) {
try { console.error(msg); }
catch(e) {};
}
Of course I do all of my debugging in Firefox with Firebug, and my goal is to ensure that these errors don't happen in production code so other browsers never hit the try/catch block. In rare cases where an error only happens in IE, I can throw an alert into the catch.Another little trick that I personally like because of its simplicity is the following:
Array.prototype.clear = function(){this.length=0;}
Clears an Array the easy way. Array.prototype.clear = function() {this.length = 0};
var a = [1,2,3];
var s = "";
for(var i in a) s += i +",";
alert(s);//1,2,3,clear
(not that many people use it this way, but since we're on the topic of little known tricks about js, I think it's worth mentioning) { 'data': data, 'left': null, 'right': null }
When you insert a new child, you can just push it onto the end of the array, find its parent by comparing the 'data' values, and set the parent's 'left' or 'right' to point to the child's index in the array. Update the child's 'left' or 'right' as needed if the child isn't a leaf.He mentions that you can build a traditional binary tree out of objects, but he makes it sound a lot more complicated than it actually is. His implementation of a binary tree breaks down at a scale where it's probably simpler and nearly as fast to just use an associative array and linear traversal, and he still requires writing the insertion, removal, and traversal methods.
If you look at the indices it will look like you have a bunch of undefined elements in the array, but no memory has actually been consumed for those indices in the array so nothing is wasted.
I understand that this is 2006 article. but this is not sound advise today. It is fine for the service (server) to order data, but one should not rely on the fact that the service will do so as many times reordering data is a function of the display (view).
var queue = [];
queue.add = queue.push;
queue.remove = queue.shift;
Oh god no! I pray I don't ever have to work with this guy after he learns about prototypes.Also, he is using "shift" and requires binary trees, but makes no mention whatsoever of the performance characteristics?