Skip to content

Instantly share code, notes, and snippets.

@Sljubura
Sljubura / main.js
Last active December 12, 2015 04:49
Efficient array iterator.
var arr = [],
i = arr.length;
while (i--) {
// Do something with array.
}
@Sljubura
Sljubura / main.js
Created February 10, 2013 15:33
Nice example of hoisting and how local foo() function definition is pushed to the top of the scope, while the definition of bar() is not hoisted. Only its declaration.
function foo() {
console.log('global foo');
}
function bar() {
console.log('global bar');
}
function hoistMe() {
console.log(typeof foo); // "function"
console.log(typeof bar); // "undefined"
@Sljubura
Sljubura / main.js
Created February 10, 2013 17:35
Callback pattern when passing method of an object.
// Calling callback when callback is a method of an object
var myapp = {};
myapp.elementHeight = 100;
myapp.setElementHeight = function (node) {
node.style.height = this.elementHeight;
};
// Where setElementHeight is our callback
// findNodes() function which call our callback
var findNodes = function (callback) {
@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!");
}
@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 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 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 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 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 / 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]');