Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created May 24, 2012 19:42
Show Gist options
  • Select an option

  • Save cowboy/2783786 to your computer and use it in GitHub Desktop.

Select an option

Save cowboy/2783786 to your computer and use it in GitHub Desktop.
stupid JavaScript-related tweets
// http://twitter.com/cowboy/status/205726591622463488
// http://twitter.com/cowboy/status/205726020026904579
Whee (function(p,q,r){r=p[q];p[q]=function(){return Math.random()<0.9?r.apply(this,arguments):'[object WAT]'}}(Object.prototype,'toString'))
// http://twitter.com/cowboy/status/196967238710992896
You know what sucks? function F(){} var f = new F(); F.prototype = {}; f instanceof F // false #javascript
// http://twitter.com/cowboy/status/196961893502566400
Fun with functions: '$'.constructor((new(new(new(new(new function $(){$.prototype.$=$.toString=$;return':)'}).$).$).$).$).$) #javascript
// http://twitter.com/cowboy/status/178158737276674049
Commafy a num, pt.2: (n+'').replace(/\..*|\B(?=(\d{3})+(\.|$))/g,function(d){return d||','}) #javascript +@JugglinMike @JFSIII @unscriptable
// http://twitter.com/cowboy/status/177496988575735810
Commafy a num, (n+'').split('.').map(function(s,i){return i?s:s.replace(/(\d)(?=(?:\d{3})+$)/g,'$1,')}).join('.'); #javascript +@JugglinMike
// http://twitter.com/cowboy/status/169156676937728001
Async boilerplate: function doSomethingLater(callback) { setTimeout(callback, Math.random() * Infinity); } #javascript /cc @ultror
// http://twitter.com/cowboy/status/168825656350740482
{}+{} //NaN, ({}+{}) //[object Object][object Object], (function(_){Object.prototype.toString=function(){return _=_?'=D':'8='}}()); ({}+{})
// http://twitter.com/cowboy/status/160766206469611520
Heh. Function.prototype.toString = function() { return this(); }; var greeting = function() { return "hello"; }; greeting + "!" #javascript
// http://twitter.com/cowboy/status/151790124206792704
Lightweight JavaScript error objects: throw {name: "MyError", message: "The error message", toString: Error.prototype.toString}; #javascript
// http://twitter.com/cowboy/status/135032652439896064
function cyber(){var a,s,l=a=s=0.014492753623188406;return a/s/l;} #javascript
// http://twitter.com/cowboy/status/129267365094506496
Method overloading for JavaScript. http://t.co/I80IBv0P #javascript #pleasedontuse
// http://twitter.com/cowboy/status/125996366001487872
Wheee (function(_){return _.call.bind(_)}({}.toString))(["awesome"]) or maybe with({$:{}.toString})$.call.bind($)(["awesome"]) #javascript
// http://twitter.com/cowboy/status/125423802975928320
Tee hee. var _=Date.call,bind=_.bind(_.bind),call=bind(_,_),apply=bind(_,_.apply); #javascript /cc @rwaldron @littlecalculist
// http://twitter.com/cowboy/status/120603078125817856
Sweet. document.querySelectorNone = function() { return null; }; #javascript #dom
// http://twitter.com/cowboy/status/99566091466047489
Array.prototype.realEvery = function(fn) { return this.concat(true).every(fn); }; /cc @kitcambridge
// http://twitter.com/cowboy/status/98409751540535296
(function(f,i,c){f.i=i;f.c=c;f.call(f);}(function(){--this.i&&this.call(this,this.c.call(this))},10,function(){console.log(this.i);}));
// http://twitter.com/cowboy/status/98095898206273536
function Abstraction(v){this.if=function(v){this.v=v;return this;};this.then=function(fn){this.v&&fn();return this;};this.if(v)} +@rwaldron
// http://twitter.com/cowboy/status/96654460109070336
Ok, better: function dateIfValid(y,m,d){var _=new Date(y,--m,d);return!isNaN(+_)&&_.getFullYear()==y&&_.getMonth()==m&&_.getDate()==d&&_}
// http://twitter.com/cowboy/status/84004164857888768
I know I fooled around with this a year ago, but it's still good stuff... var a=1, b=2; a=[b,b=a][0]; [a, b] // [2, 1] #javascript #awesome
// http://twitter.com/cowboy/status/76397138124353536
(function(s,r){return (s+'!@#B@#$A#$%D$%^$%%A%*(!$S@#$%S$%&*('.split(r)).replace(r,s)})('',/\W/g) #javascript
// http://twitter.com/cowboy/status/76396439495917568
'!@#B@#$A#$%D$%^$%%A%*(!$S@#$%S$%&*('.split(/\W/).join('') #javascript
// http://twitter.com/cowboy/status/76396041632620544
$.fn.random=function(n){return this.pushStack(this.sort(function(){return 0.5-Math.random()}).slice(0,n||1))}; +@danheberden @ralphholzmann
// http://twitter.com/cowboy/status/68699646851158016
Awesome: return (+value + '') === value ? +value : value; (safely coercing strings to numbers) #javascript
// http://twitter.com/cowboy/status/65845594400362496
Randomly nest some divs: (function(s,n){while(n--){s.push($('<div/>').appendTo(s[~~(Math.random()*(s.length-1))]))}})(['body'],999); #jquery
// http://twitter.com/cowboy/status/63787476417908737
$.isDuckLike = function(obj) { return $.isFunction(obj.quack); } /cc @rwaldron
// http://twitter.com/cowboy/status/62867454300405760
Fixing some whitespace, "function" edition. s/function\s*?(\s?\w*)\(\s*(.*?)\s*\)\s*{/function$1($2) {/g #javascript #whitespace
// http://twitter.com/cowboy/status/61468997085954048
JavaScript: Duck Punching vs Dick Punching http://bit.ly/f3vdCW #javascript
// http://twitter.com/cowboy/status/61425105347158016
So IIFE = Immediately Invoked Function Expression, but Monsterface = Empty-Crockford-IIFE (function(){}()); http://bit.ly/c4jola #IIFE
// http://twitter.com/cowboy/status/61422299500380160
I think I might start using (function(){}()) instead of (function(){})() and not just because (){}() looks like a funny monster face #IIFE
// http://twitter.com/cowboy/status/61200623504334848
I've been using $.proxy on jQuery collections lately, like: var body = $("body").hide(); setTimeout($.proxy(body, "show"), 1000); #jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment