Skip to content

Instantly share code, notes, and snippets.

@jnvm
jnvm / privatizer.js
Last active February 7, 2018 04:49
privatizer.js
/*
given goals:
https://github.com/tc39/proposal-class-fields/blob/master/PRIVATE_SYNTAX_FAQ.md#why-is-encapsulation-a-goal-of-this-proposal
& given example:
https://github.com/tc39/proposal-class-fields/blob/master/PRIVATE_SYNTAX_FAQ.md#how-can-you-provide-hidden-but-not-encapsulated-properties-using-symbols
Hidden & encapsulated values can be shared with same instances via WeakMap & Proxies
Undesirable access via callbacks passed in & whatnot just need to be defended against.
One could imagine wanting to allow callbacks read access to privates but not write, doable w/a proxy & setup condition.
@jnvm
jnvm / a-privatizer.js
Last active February 7, 2018 04:51
a privatizer
/*
given goals:
https://github.com/tc39/proposal-class-fields/blob/master/PRIVATE_SYNTAX_FAQ.md#why-is-encapsulation-a-goal-of-this-proposal
& given example:
https://github.com/tc39/proposal-class-fields/blob/master/PRIVATE_SYNTAX_FAQ.md#how-can-you-provide-hidden-but-not-encapsulated-properties-using-symbols
Hidden & encapsulated values can be shared with same instances via WeakMap & Proxies
Undesirable access via callbacks passed in & whatnot just need to be defended against.
One could imagine wanting to allow callbacks read access to privates but not write, doable w/a proxy & setup condition.
@jnvm
jnvm / frozenWeakmapEncapsulation.js
Last active February 5, 2018 06:18
frozen weakmap encapsulation
/*
https://github.com/tc39/proposal-class-fields/blob/master/PRIVATE_SYNTAX_FAQ.md#how-can-you-model-encapsulation-using-weakmaps
"There's a potential pitfall with this approach, though"
...so prevent it!
*/
const Person = (function(){
const privates = new WeakMap;
let ids = 0;
return class Person {
constructor(name, makeGreeting) {
@jnvm
jnvm / dontworry.js
Last active August 5, 2016 05:32
weird looking js
//guess what this outputs
var
varᅚ = 1,
ᅛ = 4,
ᅜ = 2,
ˋhey,ˋ=`B ‮‮`,<!--#wtf
donˈtᅠworryǃ="‮("
console.log(ˋhey,ˋ + donˈtᅠworryǃ + varᅚ*ᅛ*ᅜ)
@jnvm
jnvm / samebind.js
Last active August 3, 2016 01:20
in which jnvm makes a fool of himself claiming: bind modifies function, doesn't return new one
var echo=function(){
var echoes=echo.count||10
while(echoes--) this.makeNoise()
}
var dog={
sound:"woof",
makeNoise:function(){ console.log(this.sound) },
}
var durations={}
,os={}
function aSet(){
return new Promise((res,rej)=>{
var N = n = 10,
m = 6
while(m--) {
//increase keys
N *= 10
n = N
@jnvm
jnvm / fiddle.html
Created July 7, 2016 02:46
SNES-style gamepad controller. Implement interaction as needed.
<html>
<head>
<script src="https://wzrd.in/standalone/nipplejs"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.14/peer.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<body>
<style>
body {
@jnvm
jnvm / ShowValidStartingIdentifierChars.js
Created July 5, 2016 05:41
live-test & show valid starting identifier characters in js.
+function ShowValidStartingIdentifierChars(){
var validStarters={}
,max=130000
,group=5000
for(var i=0;i<max;i++){
try{
var s=String.fromCodePoint(i)
eval(`let ${s}=1`)
validStarters[s]=1
}