Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
DavidBruant / gist:877797
Created March 19, 2011 21:00
Wrapped setTimeout+clearTimeout
(function(global){
var setTimeout = global.setTimeout;
var clearTimeout = global.clearTimeout;
var timeoutIdentityObjects = [];
global.setTimeout = function(f, t){
var timeoutIdentityObject = new Object; // Insisting on the object being new and different from all previously generated
var i = setTimeout(function(){
f.call(); // Incomplete, because doesn't take strings and optinal arguments into consideration
@DavidBruant
DavidBruant / gist:904114
Created April 5, 2011 17:52
Default fowarding fix trap
// Object.{freeze|seal|preventExtensions}(proxy) -> proxy
fix: function() {
// As long as target is not frozen, the proxy won't allow itself to be fixed
if (!Object.isFrozen(this.target))
return undefined;
var props = {};
var handler = this;
Object.getOwnPropertyNames(this.target).forEach(function (name) {
var desc = Object.getOwnPropertyDescriptor(this.target, name);
// turn descriptor into a trapping accessor property
@DavidBruant
DavidBruant / gist:905301
Created April 6, 2011 07:59
C instances communicating with each other
"use strict";
var getInstance = (function(){
var p = Object.create(null);
return function(){
return Object.create(p);
};
})();
var c1 = getInstance();
Proxy.ForwardingHandler.prototype = {
/***
** all traps use this.state.get[proxy] to retrieve the target
*/
state: new WeakMap();
};
function ForwardingPair(target){// Could be Proxy.ForwardingPair too...
var h = new Proxy.ForwardingHandler(); // Just a new object identity inheriting from all methods
var p = Proxy.create(h);
@DavidBruant
DavidBruant / gist:1016007
Created June 9, 2011 03:41
(document.querySelectorAll('*')).forEach ?
var els = document.querySelectorAll('a');
els.forEach(function(e){console.log(e.href);}); // TypeError: els.forEach is not a function
// Why?
// document.querySelectorAll returns a NodeList.
// First thing first, as opposed to other NodeList, this one is not live, mostly because the
// implementation of a live object would be terribly complicated for some selectors
// NodeList are a DOM interface introduced in DOM core level 1 (random guess)
@DavidBruant
DavidBruant / gist:1026960
Created June 15, 2011 12:11
Proxy pretending to be arrays
(function(global){
// Place to store fake arrays
var myProxyArrays = new WeakMap();
global.ProxyArray = (function(){
var handler, call, construct;
// See https://github.com/DavidBruant/ProxyArray for actual implementation
return function(){
var p = Proxy.createFunction(handler, call, construct); //
function randomColorString(){
var r, g, b;
r = Math.floor(256*Math.random()).toString(10);
g = Math.floor(256*Math.random()).toString(10);
b = Math.floor(256*Math.random()).toString(10);
return ('rgb('+ r +','+ g +','+ b +')');
}
@DavidBruant
DavidBruant / gist:1149046
Created August 16, 2011 13:13
bound functions and instanceof
function f(){}
var g = f.bind({});
var newg = new g;
// call g.[[Construct]] (which is f.[[Construct]] per ES5.1 15.3.4.5 step 13)
// so, |new g| is the same as |new f|: an object which has f.prototype as [[Prototype]]
newg instanceof g;
// instanceof g calls the g.[[HasInstance]](newg) (please forgive my skip of reference/GetValue mess)
// As per ES5.1 - 15.3.4.5.3, g.[[HasInstance]](V) is g.[[TargetFunction]].[[HasInstance]](V)
var keys = [];
var n;
var P = Object.getPrototypeOf;
var o = window;
// Retrieving all property names of the global object,
// because proto chain are messy and different in all browsers
while(o !== null){
keys = keys.concat(Object.getOwnPropertyNames(o));
o = P(o);
@DavidBruant
DavidBruant / gist:1306146
Created October 22, 2011 16:06
Link checker
var links = document.getElementsByTagName('a');
console.log(links.length);
var workingUrls = [],
brokenUrls = [];
function l(e){
var t = e.target;
var url = t.src;
console.log(url, e.type);