Skip to content

Instantly share code, notes, and snippets.

View jherax's full-sized avatar
👾
Performance focused

David Rivera jherax

👾
Performance focused
View GitHub Profile
@jherax
jherax / falsy-values.txt
Last active April 13, 2016 18:06
JavaScript coercion
VALOR COERCIÓN
------- ---------
false false
0 false
“” false
NaN false
null false
undefined false
@jherax
jherax / js-1-inheritance.js
Last active September 24, 2018 18:09
JavaScript OOP: Herencia y Prototipos
//definimos el constructor del objeto base y
//establecemos las propiedades por instancia
function Person (name, age) {
"use strict";
this.name = name || "unnamed";
this.age = +age || 0;
}
//definimos el prototipo del objeto base
//estableciendo las propiedades compartidas
@jherax
jherax / sortBy-old.js
Last active November 8, 2018 04:01
Sorting Arrays
/*
* Important!
* This snippet is deprecated, a best implementation of sortBy can be found here:
* https://github.com/jherax/array-sort-by
*/
var sortBy = (function () {
var toString = Object.prototype.toString,
// default parser function
parse = function (x) { return x; },
@jherax
jherax / js-delete-properties.js
Created May 26, 2015 03:14
JavaScript: Eliminar propiedades
(function() {
//verificamos cual es el ámbito global
//(generalmente el objeto window)
console.log("Global scope", this);
var x = 42; //variable local
var point = { x: 10, y: 15 }; //objeto local
//si no se especifica el keyword "var",
@jherax
jherax / createNS.js
Created May 26, 2015 04:00
JavaScript: Crear namespaces
//utility to create safe namespaces
function createNS (namespace) {
var nsparts = namespace.toString().split("."),
reName = (/^[A-Za-z_]\w+/),
cparent = window,
i, subns, nspartsLength;
// we want to be able to include or exclude the root namespace so we strip it if it's in the namespace
if (nsparts[0] === "window") nsparts = nsparts.slice(1);
// loop through the parts and create a nested namespace if necessary
for (i = 0, nspartsLength = nsparts.length; i < nspartsLength; i += 1) {
@jherax
jherax / customError.js
Last active May 30, 2020 15:43
JavaScript: Custom Error Builder
// Creates user-defined exceptions
var CustomError = (function() {
'use strict';
//constructor
function CustomError() {
//enforces 'new' instance
if (!(this instanceof CustomError)) {
return new CustomError(arguments);
}
@jherax
jherax / _parseValue.js
Last active May 30, 2020 15:42
Gets values from the url search
/* eslint-disable no-underscore-dangle */
/**
* Supported types
*/
const TYPES = {
_true: /^true$/i,
_false: /^false$/i,
_null: /^null$/i,
_number: /^[0-9]+$/,
@jherax
jherax / clone.js
Last active May 30, 2020 15:42
Clones or extends an object (deep copy) supporting objects with circular references
/**
* @author
* David Rivera (jherax)
* https://github.com/jherax
*/
/* eslint-disable no-bitwise */
/** @private */
const toString = Object.prototype.toString;
@jherax
jherax / getEventHandlers.js
Last active October 25, 2023 02:42
jQuery: Gets all event handlers bound to a DOM Element
/**
* Gets all event-handlers from a DOM element.
* Events with namespace are allowed.
*
* @param {Element} node: DOM element
* @param {String} eventns: (optional) name of the event/namespace
* @return {Object}
*/
function getEventHandlers(element, eventns) {
const $ = window.jQuery;
@jherax
jherax / $.center.js
Last active October 17, 2016 05:07
jQuery: center an element relative to another
(function ($) {
// Reverses the array of matched elements
$.fn.reverse = Array.prototype.reverse;
/**
* Centers an element relative to another.
* @signature: $(selector).center(options)
* @param {Object} options:
* Defines the options with the following properties: