Skip to content

Instantly share code, notes, and snippets.

@Sljubura
Sljubura / main.js
Last active December 22, 2015 20:38
Jednostavan prikaz closure-a.
function pdv(OPERACIJA) {
return function (broj) {
if ( OPERACIJA === '-' ) {
return broj - 20;
} else if( OPERACIJA === '+' ) {
return broj + 20;
} else {
throw 'Lose uneti parametri!';
}
}
@Sljubura
Sljubura / secondary.js
Created March 12, 2013 21:44
Strategy pattern
var data = {
first_name: "Vladimir",
last_name: "Sljubura",
age: "unknown",
username: "Slju"
};
validator.config = {
first_name: 'required',
age: 'numeric',
@Sljubura
Sljubura / main.js
Created March 5, 2013 19:56
Decorator pattern
// Constructor
function Sale(price) {
this.price = price || 10
}
Sale.prototype.getPrice = function () {
return this.price;
};
Sale.decorators = {};
Sale.decorators.statetax = {
@Sljubura
Sljubura / main.js
Created March 5, 2013 19:39
Simple iterator
var agg = (function () {
var index = 0,
data = [1,2,3,4,5],
length = data.length;
return {
next: function () {
@Sljubura
Sljubura / main.js
Created March 5, 2013 19:08
Factory pattern is soo simple in JavaScript
function CarMaker() {}
CarMaker.prototype.drive = function () {
return "Vroom, I have " + this.doors + " doors";
};
CarMaker.factory = function (type) {
var constr = type,
newcar;
if (typeof CarMaker[constr] !== "function") {
@Sljubura
Sljubura / prototypal_inheritance.js
Created March 2, 2013 14:32
Prototypal inheritance /w and /wo ECMAScript 5
function object(o) { // Simple prototypal inheritance
function Proxy() {}
Proxy.prototype = o;
return new Proxy();
}
function Person() {
this.name = "Vladimir"
}
Person.prototype.getName = function () {
@Sljubura
Sljubura / main.js
Created March 2, 2013 13:06
Class inheritance sugar
// Pattern to avoid inheritance via prototype
var klass = function (Parent, props) {
var Child, Proxy, i;
Child = function () {
if (Child.uber && Child.uber.hasOwnProperty("__construct")) { // Check if Child has own constructor in context of superclass
Child.uber.__construct.apply(this, arguments);
}
@Sljubura
Sljubura / main.js
Created March 2, 2013 12:39
Proxy inheritance
// Inheritance using proxy
function inherit(Child, Parent) {
var Proxy = function () {};
Proxy.prototype = Parent.prototype; // Set Proxy
Child.prototype = new Proxy(); // Inherit from Parent via Proxy
Child.superclas = Parent.prototype; // Allow access to Parent
Child.prototype.constructor = Child; // Set Child constructor to not point to Parent constructor
}
@Sljubura
Sljubura / main.js
Created March 1, 2013 12:48
Inherit both the properties and methods from parent.
// Pattern to inherit both the properties and methods
function Parent(name) {
this.name = name || "Vladimir";
}
Parent.prototype.say = function () {
return this.name;
};
function Child(name) {
Parent.apply(this, arguments);
@Sljubura
Sljubura / main.js
Created March 1, 2013 12:31
Multiple inheritance
// Multiple inheritance with rent-a-constructor
function Human() {
this.starfleet = true;
this.enterprize = true;
}
function Klingon() {
this.empire = true;
this.warbird = true;
}
function Worf() {