Skip to content

Instantly share code, notes, and snippets.

View karolk's full-sized avatar
💭
cooking on gas

Karol K karolk

💭
cooking on gas
View GitHub Profile
(new Array(10000)+'')
.split(',')
.map(function(){
return ~~(Math.random()*100)
})
.sort()
.reduce(function(arr, val){
arr[val]||(arr[val]=[]);
arr[val].push(val);
return arr

Write a function Value returning an object, which purpose is to store a numeric value. The value of the object will be mutable, but also protected with min and max. This should be achieved through getters and setters.

Example usage:

v = Value();
v.value
//=> 0

v2 = Value({value: 10, min: 0, max: 100});
v2.value = 101
@karolk
karolk / state-pitfalls.js
Last active December 12, 2015 04:38
Problems with state
function Closure(args) {
var refState = args[0] //some state by referencing
//completely new state
var newState = {
order: 1
}
//when objects are passed around they might end up cached elsewhere
@karolk
karolk / jquery.dom-events-generalise.js
Created December 3, 2012 14:39
generalise events hooking on DOM
$('.events-root')
.on('click', 'a[data-event], button[data-event], input[data-event]', function(e) {
E.publish( $(this).attr('data-event'), {data: [e.target]} );
e.preventDefault();
})
.on('change', 'select[data-event]', function(e) {
E.publish( $(e.target).attr('data-event'), {data: [e.target]} );
})
.on('submit', 'form[data-event]', function(e) {
E.publish( $(e.target).attr('data-event'), {data: [e.target]} );
@karolk
karolk / instanceof.js
Created November 6, 2012 23:21
How instanceof works
function A() {}
var a = new A()
a instanceof A
//true
//swap the prototype
A.prototype = {}
a instanceof A
//false
@karolk
karolk / ma.js
Created October 24, 2012 13:46
Moving average in JS
function movingAvg(series, len) {
var ret = [];
series.forEach(function(elem, i, ser) {
var localSeries = ser.slice(0, i+1), lsLen = localSeries.length
len && localSeries.splice( 0, Math.max(0, lsLen-len) );
ret.push(
Math.round(
localSeries.reduce(
@karolk
karolk / multiple inheritance.md
Created October 5, 2012 09:41
Comparison of a multiple inheritance implementation in ES3 and ES5

I am always baffled by the complicated graphs people draw to explain the ES3 concept for inheritance. I don't think inheritance is the right word for what is in the language. The more appropriate word would be composition. Prototypal composition, because an object called prototype is used to bring in that magic. If you think this means JS doesn't have inheritance, don't forget that the goal is not to be able to inherit. What we are trying to achieve is code reuse, small memory footprint and polymorhism (defined here as an ability to mutate object slightly in relation to their generic prototypes).

ES3 prototypal composition pattern

ES3 in it's attempt to imitate Java (for familiarity purposes) emphasised the role of the class in instances creation. The idea was/is as follows

//1. Functions also serve as classes. There is no separate `class` keyword.

function Animal( sound ) {
@karolk
karolk / e.js
Created September 21, 2012 14:27
pubsub implementation with data passing and sticky events
/**
* Pubsub event management
* Dependencies: none
* Autor: Karol Kowalski
* Repo: https://gist.github.com/3761754
*/
(function(host) {
var events = {},
@karolk
karolk / is.js
Created July 27, 2012 15:08
checking 'deep' namespaces for existence and type
(function(host) {
host.is = function is(namespace, type) {
var ret = true,
parent = host,
chunks = namespace.split('.');
chunks.forEach(function(e, i) {
if ( parent === null ) { //this has to be straight equality check, can't lookup properties on null
ret = false
return //break
@karolk
karolk / get-set-property-descriptors-example.js
Created July 25, 2012 17:19
new property descriptors syntax in javascript includes getters and setters
var t = Object.create({}, {
__percent__: {
value: 0.01,
writable: true
},
percent: {
get: function() {
return this.__percent__*100
},
set: function(v) {