Skip to content

Instantly share code, notes, and snippets.

@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
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 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 / gist:4973780
Last active December 13, 2015 20:58
Some jQuery tips.
// Always use lates version
// Not all selectors are equaly fast
// Fastest to slowest selectors.
$('#selector-by-id');
$('div', 'input');
$('.class-selectors');
// And the slowest are pseudo selectors
$(':visible, :hidden, [attribute=value]');
@Sljubura
Sljubura / main.js
Created February 16, 2013 12:38
Sandbox pattern.
function Sandbox() {
// turning arguments into an array
var args = Array.prototype.slice.call(arguments),
// last argument is the callback
callback = args.pop(),
// modules can be passed as an array or as individual parameters
modules = (args[0] && typeof args[0] === "string") ? args : args[0],
i;
@Sljubura
Sljubura / main.js
Created February 16, 2013 11:49
Module pattern with constructor.
// Module pattern that creates constructor function.
APP.utilities.Array = (function (app, global) {
// Dependencies
var uobj = APP.utilities.object,
ulang = APP.utilities.lang,
// Private properties
Constructor;
@Sljubura
Sljubura / main.js
Created February 16, 2013 11:42
Module pattern
// Module pattern
APP.utilities.array = (function () {
// Private properties
var array_string = "[object Array]",
ops = Object.prototype.toString(),
// Private methods
inArray = function (haystack, needle) {
for (var i = 0; i < haystack.length; i = i + 1) {
@Sljubura
Sljubura / main.js
Created February 15, 2013 19:48
'Private' properties.
// Implement private members with closure
function Person() {
var name = 'Frank';
this.getName = function () { // Privileged method, has access to private property.
return name;
}
}
var user = new Person();
console.log(user.name); // undefined
@Sljubura
Sljubura / main.js
Created February 15, 2013 15:15
Name-spacing
// Clean globals with name-spacing pattern
// Anti-pattern
// Pollution with 5 objects added to global.
function add() {}
function subtract() {}
var container = {};
var data = {};
data.person = {name: 'John', email: 'smith@gmail.com'};
data.account = {};
@Sljubura
Sljubura / main.js
Created February 12, 2013 19:26
Self-defining functions.
// Self-defining functions
var greetings = function () {
console.log("Hello!");
greetings = function () {
console.log("Goodbye!");
}