This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.defineProperty(window, "__", {set: Object.freeze}); | |
// ex usage: | |
var state= __={ | |
a: 1, | |
b: 3, | |
c: __=[1,2,3, __={ z: 7 } ] | |
}; | |
// try to mutate: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<vcc-demo></vcc-demo> | |
<script src="http://danml.com/bundle/rndme.cia_rndme.vcc_.js"></script> | |
<script> | |
var store = CIA({ | |
INC: function(state, n) { | |
state.index+= (n||1); | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// uses arrow functions to define methods and creates an un-breakable bind to `this` | |
var x=new function(){ | |
Object.assign(this, { | |
val : 530, | |
get: ( )=> this.val, | |
set: (v)=> this.val=v, | |
}); | |
}; | |
x.get(); // 530 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//enough parens here to compile lisp apps... | |
var x= Object.freeze(((val)=>({ | |
get: ( )=> val, | |
set: (v)=> val=v, | |
}))(123)); | |
x.get(); // 123 | |
x.set(321); // 321 | |
x.get(); // 321 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cls(fn, state){ | |
return Object.freeze(fn.call(state)); | |
} | |
var x= cls(function(){return{ | |
get: ( )=> this.val, | |
set: (v)=> this.val=v, | |
}}, { | |
val: 654 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// uses arrow functions to define methods seperatly from data, like an alternative to .prototype. | |
var x=function(){ | |
return { // public methods: | |
get: ( )=> this.val, | |
set: (v)=> this.val=v, | |
}; | |
}.call({ // private state: | |
val: 902 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function freeze(ob) { | |
for(var k in ob) { | |
if(!freeze.hasOwnProperty.call(ob, k)) continue; | |
if(typeof ob[k] === "object" && ob[k]) freeze(v); | |
} | |
return Object.freeze(ob); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//a promise-based ajax io | |
function pGet(url, data) { | |
return new Promise(function(y, n) { | |
var e = new XMLHttpRequest(); | |
e.responseType="document"; | |
e.onload=x=>y(e.response); | |
if(n) e.onerror=x=>n(e); | |
e.open(data?"POST":"GET", url, true); | |
setTimeout(e.send.bind(e,data), 0); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// public domain, from wikipedia description: https://en.wikipedia.org/wiki/RC4#Spritz | |
function rc4(key, str) { | |
var s = [], | |
j = 0, | |
x, res = [], | |
i, j, y, l = key.length, | |
m = str.length; | |
for(i = 0; i < 256; i++) s[i] = i; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//public domain, based on https://www.schneier.com/blog/archives/2014/10/spritz_a_new_rc.html | |
function spritz(key, str) { | |
var s = [], | |
j = 0, | |
w = 17, | |
k = 0, | |
res = [], | |
l = key.length, | |
m = str.length, |