Skip to content

Instantly share code, notes, and snippets.

View fredyang's full-sized avatar

Fred Yang fredyang

View GitHub Profile
@fredyang
fredyang / gist:1865966
Created February 19, 2012 21:42
isAncestor and isDescendant
$.fn.isAncestor = function( elem ) {
var $elem = $( elem );
if (this.is( $elem )) {
return false;
}
while (($elem = $elem.parent()).length) {
if (this.is( $elem )) {
return true;
}
@fredyang
fredyang / pipleline
Created February 6, 2012 05:55
help you simulate the pipeline in other functional language
function makepipe ( initValue ) {
var result = initValue,
slice = [].slice,
rtn = function( fn ) {
result = fn.apply( null, [result].concat( slice.call( arguments, 1 ) ) );
return rtn;
};
rtn.get = function() {
@fredyang
fredyang / userGlobal
Created December 20, 2011 20:35
display how many global variable is used by user (not system)
(function() {
var keys = [];
for (var k in window) {
keys.push(k);
}
function isUserGloba(k) {
for (var i = 0; i < keys.length; i++) {
if (keys[i] === k) {
@fredyang
fredyang / silly-array-shuffle.js
Created December 8, 2011 15:16 — forked from cowboy/silly-array-shuffle.js
JavaScript: Silly Array Shuffle
// Array.indexed(4) returns [0, 1, 2, 3]
Array.indexed = function(n) {
var result = [];
while (n--) {
result[n] = n;
}
return result;
};
// Shuffle an array in a not too efficient way.
@fredyang
fredyang / gist:1447252
Created December 8, 2011 15:15
check how many global variable is used in chrome
(function() {
var keys = "allKeys,top,window,location,external,chrome,v8Locale,document,SVGPathSegLinetoVerticalRel,SVGFESpotLightElement,HTMLButtonElement,Worker,webkitIDBTransaction,webkitNotifications,EntityReference,NodeList,screenY,SVGAnimatedNumber,SVGTSpanElement,navigator,MimeTypeArray,sessionStorage,SVGPoint,SVGScriptElement,OverflowEvent,HTMLTableColElement,OfflineAudioCompletionEvent,HTMLOptionElement,HTMLInputElement,webkitIDBIndex,SVGFEPointLightElement,SVGPathSegList,SVGImageElement,HTMLLinkElement,defaultStatus,MutationEvent,HTMLMetaElement,XMLHttpRequestProgressEvent,WebKitCSSTransformValue,Clipboard,HTMLTableElement,SharedWorker,SVGAElement,SVGAnimatedRect,webkitIDBDatabaseError,HTMLSpanElement,SVGGElement,toolbar,SVGLinearGradientElement,innerHeight,webkitIndexedDB,SVGForeignObjectElement,SVGAnimateElement,applicationCache,SVGFontElement,webkitAudioContext,pageXOffset,SVGFontFaceElement,ErrorEvent,Element,SVGPathSegCurvetoQuadraticSmoothRel,opener,SVGStopElement,HTMLUnknownElement,StyleShe
@fredyang
fredyang / jquery.ba-simple-ajax-mocking.js
Created December 8, 2011 02:52 — forked from cowboy/jquery.ba-simple-ajax-mocking.js
Simple jQuery (1.5+) AJAX Mocking (requires JSON, tested in jQuery 1.7))
/*!
* Simple jQuery (1.5+) AJAX Mocking - v0.1.0 - 11/16/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($) {