Untitled
Untitled
of all the arguments // even those that your function doesn't "accept" return arguments; // ['first argument', 2, 'I am a third argument'] })('first argument', 2, 'I am a third argument');
// gotcha /* If a function returns nothing, undefined is returned So don't use functions as truthy falsy testers unless you return a truthy. */ (function() { var page_count = 0; if ( page_count++ ) { [Link]('I will never run') } })();
// Bad parts //================================= // Arrays convert all values to strings before // sorting, so you can end up with this (function() { sorted_array = [1,22,13,4,5].sort(); [Link](sorted_array); // [1, 13, 22, 4, 5] // but you can tell it how to // sort smartly sorted_array = [1,22,13,4,5].sort(function(a, b) { // // // // you get a and b to sort. if the function returns a.. negative number: a comes ahead in sort order a positive number: b comes first
// // // The likelihood a program will work is significantly enhanced by our ability t o read it // I think about comments as a time machine that i use to send important message s to future me. // // ******************* // JS has functional scope, not block scope like C // sometimes people make this mistake: (function() { if (true) { var foo = 'apple'; } // foo isn't local // block scope !== functional scope [Link](foo); // 'apple' })(); // // ***************** // There are ten fucking thousand reserved words // and they may trip up your dot syntax (function() { var available_sizes = { short: 2, medium: 5, long: 10 }; // in dumb browsers long // throws an error // [Link](available_sizes.long); })();
// // ***************** // parseInt needs two params or it will // fuck you over (function() { // convert a string to a number. // it uses base 10 by default var foo = parseInt('78'); // 78 // repeat, with a different number // this time it's base 8 by default? var wtf = parseInt('08'); // 0 // always provide parseInt() // with a base 10 param
// var allbetter = parseInt('08', 10); // 10 })(); (function() { myvar = "boolean break byte case catch char class const continue debugger default delete do double else enum export extends false final finally float for function goto if implements import in instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var volatile void while with"; })();