Skip to content

Instantly share code, notes, and snippets.

View nfroidure's full-sized avatar
🌻
Getting greener

Nicolas Froidure nfroidure

🌻
Getting greener
View GitHub Profile
@nfroidure
nfroidure / gist:5336601
Created April 8, 2013 12:57
So nice to get proxies like that
Object.defineProperty(o, /^[0-9]+$/, {
get : function() { },
set : function() { },
enumerate : function() { },
writable : false,
configurable : false,
enumerable : false
});
@nfroidure
nfroidure / Queue.js
Last active December 31, 2018 16:36
A simple Javascript Queue based on an Array wrap. More explanations here : https://insertafter.com/blog-fifo_lifo_javascript.html
class Queue {
constructor(...elements) {
// Initializing the queue with given arguments
this.elements = [...elements];
}
// Proxying the push/shift methods
push(...args) {
return this.elements.push(...args);
}
shift(...args) {
@nfroidure
nfroidure / Queue.js
Last active December 31, 2018 16:43
A simple Javascript Queue based on an Array wrap and using setters/getters. More explanations here : https://insertafter.com/blog-fifo_lifo_javascript.html
class Queue {
constructor(...elements) {
// Initializing the queue with given arguments
this.elements = [...elements];
}
// Proxying the push/shift methods
push(...args) {
return this.elements.push(...args);
}
shift(...args) {
@nfroidure
nfroidure / Stack.js
Last active December 31, 2018 16:39
A simple Javascript Stack based on an Array wrap. More explanations here : https://insertafter.com/blog-fifo_lifo_javascript.html
class Stack {
constructor(...elements) {
// Initializing the stack with given arguments
this.elements = [...elements];
}
// Proxying the push/shift methods
push(...args) {
return this.elements.push(...args);
}
pop(...args) {
@nfroidure
nfroidure / SimpleSingleton.js
Last active December 31, 2018 17:30
Part of my blog post Revisiting Singleton Design Pattern : https:///insertafter.com
// Singleton pattern
var MySingleton=(function() {
// creating a variable to contain the instance
var instance=null;
// here goes private stuff
var _myPrivateVar=1;
var _myPrivateFunction=function(){
console.log('In private function.');
};
// creating singleton constructor
@nfroidure
nfroidure / InheritSingleton.js
Created May 30, 2013 07:52
Part of my blog post Revisiting Singleton Design Pattern : http:///www.insertafter.com
// Inherit JavaScript Singleton
function ParentConstructor() {}
ParentConstructor.prototype.publicProperty1=1;
var InheritSingleton=(function(parentConstructor) {
// creating a variable to contain the instance
var instance=null;
// creating singleton constructor
function Constructor() {
// assigning instance to our variable
@nfroidure
nfroidure / gist:5696893
Last active December 18, 2015 00:29 — forked from brunobord/gist:5695039
// -------- Database section
// current version
var __versions__ = ['1.0.1','1.0.2','1.0.3'];
var __version__ = localStorage.getItem('db:version')
||__versions__[__versions__.length-1];
function migrate_1_0_1() {
console.log('going into migrate_1_0_1');
}
function migrate_1_0_2() {
@nfroidure
nfroidure / random.js
Created June 3, 2013 11:10
Getting Math.random a bit more random
Math.random=(function(rand) {
var salt=0;
document.addEventListener('mousemove',function(event) {
salt=event.pageX*event.pageY;
});
return function() { return (rand()+(1/(1+salt)))%1; };
})(Math.random);
@nfroidure
nfroidure / Promise.js
Last active March 15, 2019 09:51
A simple Promise implementation. Moved to GitHub : https://github.com/nfroidure/Promise/blob/master/Promise.js
var Promise=(function() {
var AWAIT=0, SUCCESS=1, FAIL=-1;
function Promise(logic) {
var promise=this;
promise.solved=AWAIT;
var success=function (value) {
if(AWAIT!==promise.solved)
return;
promise.solved=SUCCESS;
promise.value=value;
//addEventListener polyfill 1.0 / Eirik Backer / MIT Licence
(function(win, doc){
if(win.addEventListener)return; //No need to polyfill
function docHijack(p){ doc[p]=(function(old){ return function(v){return addListen(old(v))}; })(doc[p]);}
function addEvent(on, fn, self){
return (self = this).attachEvent('on' + on, function(e){
var e = e || win.event;
e.preventDefault = e.preventDefault || function(){e.returnValue = false}
e.stopPropagation = e.stopPropagation || function(){e.cancelBubble = true}