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
@karolk
karolk / temple-template.html
Created April 2, 2012 21:38
temple.js templating example
<h1 data-templ="header"></h1>
<ul data-templ="items">
<li>
<strong data-templ="strong name"></strong>
<a href="#" data-templ="url name"></a>
</li>
</ul>
<p data-templ="empty">The list is empty.</p>
@karolk
karolk / isEmptyNullUndef.js
Created April 25, 2012 11:56
check if a value is null, undefined empty string or an empty array
//returns true if [], '', null, undefined
//returns false if 0, false, {}, NaN
function isEmptyNullUndef(value) {
return [value] == '';
}
@karolk
karolk / wordpress vulnerability tokens
Created May 1, 2012 14:27
tokens to grep for in wordpress code to check if it was hacked into
eval(
auth_pass
_0x4
WordPress\.Org
5db4c956bb56f6f050412fecd239344f
hgerwhu45
maridora
strrev
base64_decode
decrypt
@karolk
karolk / ie6-throttle.js
Created July 4, 2012 13:32
Detect that the user is running IE6 and sets the screen update rate at 1 refresh per 10 seconds. This makes the website look very slow. The setting will continue to work in a browser window even if the user visits a different website as IE6 sets this for
navigator.userAgent.toLowerCase().indexOf("msie 6.")!=-1&&(screen.updateInterval=10000);
@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) {
@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 / 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 / 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 / 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 / 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