Skip to content

Instantly share code, notes, and snippets.

@Sljubura
Sljubura / main.js
Created February 19, 2013 18:29
Simple chaining
// Chaining pattern
var main = {
index: 10,
addToIndex: function () {
this.index += 10;
return this; // return instance of the object and allow chaining
},
subtractFromIndex: function () {
this.index -= 10;
return this; // return instance of the object and allow chaining
@Sljubura
Sljubura / main.js
Last active December 13, 2015 22:59
Pretty prototype
if (typeof Function.prototype.method !== "function") { // Check if method is already implemented
Function.prototype.method = function (name, implementation) {
this.prototype[name] = implementation; // Add implementation to prototype
return this;
};
}
// Pretty prototype
var User = function (name) {
this.name = name;
@Sljubura
Sljubura / main.js
Created March 1, 2013 12:31
Inheritance
// Inheritance, rent-a-constructor versus prototype based inheritance.
function Parent() {
this.list = ['work', 'friends'];
}
var parent = new Parent();
// Events inherits from a parent object by referencing properties
function Events() {
Events.prototype = parent; // or new Parent() if instance is not available
}
@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() {
@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 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 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 / 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 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 / 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 () {