Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / queue.LAB.js
Created November 17, 2010 22:20
adding "queueing" to LABjs dynamically, to be able to simulate the $LAB chain dynamically with a for-loop
<!DOCTYPE html>
<html>
<head>
<title>LABjs Demo</title>
<script src="/js/queue.LAB.js"></script>
<script>
// adding `false` in the queue will signifiy an empty .wait() in the $LAB chain
$LAB.queue("http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js", false);
$LAB.queue("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js", false);
@getify
getify / gist:710231
Created November 22, 2010 16:48
how to detect IE from JavaScript using conditional comments
// stripped down version just detecting IE
(function(global){
global._isIE = false;
try {
var div = document.createElement("div");
div.innerHTML = "<!--[if IE]><i></i><![endif]-->";
global._isIE = (div.getElementsByTagName("i").length > 0);
} catch(err) { }
})(window);
@getify
getify / gist:713779
Created November 24, 2010 15:09
loading google analytics using LABjs
<!DOCTYPE html>
<html>
<head>
<title>LABjs Demo</title>
</head>
<body>
<!-- some stuff -->
<script src="/js/LAB.js"></script>
@getify
getify / auto-ga.js
Created November 24, 2010 15:25
create an auto-loader for Google Analytics (using LABjs)
/*! LAB.js v1.0.3 (c) Kyle Simpson MIT License */
/* INCLUDE THE MINIFIED LAB.js CODE HERE */
$LAB
.script('https:'==document.location.protocol?'https://ssl':'http://www')+'.google-analytics.com/ga.js')
.wait(function(){
var scripts = document.getElementsByTagName("script"),
src, i, len,
acct = "UA-XXXXX-X",
@getify
getify / ex1:basic_@_usage
Created December 3, 2010 17:10
theoretical native promise/defer via @ operator (in JavaScript or something like it)
//SUMMARY:
// a() @ b() ==> execute a(). if a() flags an async deferral inside it,
// then wait to continue execution of the expression until that promise is
// fulfilled, then continue execution *AT* b().
//
// more generally: X @ Y ==> evaluate X expression. if it was a function call
// call that deferred with a promise, wait until fulfilled then continue at
// Y. otherwise, assume that X had an implicit immediately fulfilled promise,
// and continue evaluating at Y.
// ------
In "Safari 5.0.1" on Win7:
> console.log(navigator.userAgent);
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8
In "Webkit Nightly r74232" on Win7:
> console.log(navigator.userAgent);
v8::HandleScope handle_scope;
v8::Context::Scope scope(my_context);
v8::Handle<v8::Function> func1;
{
v8::TryCatch try_catch;
v8::Handle<v8::Script> func1_script = v8::Script::Compile(v8::String::New("func1"), v8::String::New("inline::code"));
v8::Handle<v8::Value> func1_res = func1_script->Run();
func1 = v8::Handle<v8::Function>::Cast(func1_res);
}
@getify
getify / gist:801962
Created January 29, 2011 16:24
explaining __proto__ in prototypal inheritance
function A() {}
var a = new A();
function B() {
function F(){}
F.prototype = a;
return new F();
}
var b = new B(); // `b` now inherits from `a` (*not* `A`)
@getify
getify / gist:802625
Created January 30, 2011 06:35
preserving error context in a try/catch re-throw
function foo() {
// ...
nonExistent();
//
}
function bar() {
try {
foo();
}
var function_sandbox = (function(global) {
function SubFunc(func) {
func.__proto__ = funcPlugin;
return func;
}
var funcPlugin, __Function = global.Function;
// make prototype values conform to ECMA spec and inherit from regular natives
(SubFunc.prototype = new __Function('')).__proto__ = __Function.prototype;