Skip to content

Instantly share code, notes, and snippets.

@julienetie
julienetie / prototypal_Inheritance
Created February 27, 2015 17:11
Prototypal Inheritance
//From http://youtu.be/PMfcsYzj-9M?t=14m48s
var AnswerPrototype = {
constructor: function fn0(value) {
this._val = value;
},
get: function fn1() {
return this._val;
}
};
@julienetie
julienetie / Get_Numbers_From_Array
Created March 24, 2015 21:33
Get Numbers From Array
// Get Numbers From Array
Array.prototype.getArrayNumbers = function(){
var _this = this;
for(var i = _this.length; i--;){
if ( typeof(_this[i]) !== 'number' ) _this.splice(i, 1);
}
return _this;
};
@julienetie
julienetie / Sort-Numerically
Created March 24, 2015 21:34
Sort Numerically
// Sort Numerically
Array.prototype.sortNumerically = function (direction) {
var _this = this;
function comparator(a, b) {
return direction < 0 ? b - a : a - b;
}
return _this.sort(comparator);
};
@julienetie
julienetie / Get-string-From Array
Created March 24, 2015 21:36
Get strings From Array
// Get strings From Array
Array.prototype.getArrayStrings = function () {
var _this = this;
for (var i = _this.length; i--;) {
if (typeof (_this[i]) !== 'string') _this.splice(i, 1);
}
return _this;
};
@julienetie
julienetie / Sort-Alphabetically
Created March 24, 2015 21:36
Sort Alphabetically
// Sort Alphabetically
Array.prototype.sortAlphabetically = function (direction) {
var _this = this;
function comparator(a, b) {
if (direction < 0) {
return a == b ? 0 : a < b ? 1 : -1;
} else {
return a == b ? 0 : a < b ? -1 : 1;
}
}
@julienetie
julienetie / indexOf()
Created April 4, 2015 19:37
ES5 indexOf()
// Production steps of ECMA-262, Edition 5, 15.4.4.14
// Reference: http://es5.github.io/#x15.4.4.14
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
// 1. Let O be the result of calling ToObject passing
// the this value as the argument.
if (this == null) {
@julienetie
julienetie / hasLocalStorage
Created April 5, 2015 18:12
Check for local storage support
function hasLocalStorage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
@julienetie
julienetie / key-controls-without-conditions
Created April 8, 2015 11:29
Key controls without conditions
window.addEventListener("keydown", function (e) {
var newKp = 'key' + e.keyCode,
keyID,
key = {
'key38': function () {
alert('up');
},
'key40': function () {
alert('down');
},
@julienetie
julienetie / style-prefix.js
Created July 22, 2015 01:56
Styles an element with a vendor prefix if required.
/**
* @author Julien Etienne
* Sets the style of an element with the appropriate browser prefix.
* @param {Object} styleObject - Style detials.
* @param {Object} styleObject.el - The element to apply the style to.
* @param {String} styleObject.style - The style that may require a prefix.
* @param {String|Number} styleObject.value - The value to be applied.
*/
function stylePrefix(styleObject) {
var element = styleObject.el,
@julienetie
julienetie / hasIOS6RequestAnimationFrameBug.js
Last active August 29, 2015 14:25
Cleanly detect requestAnimationFrame iOS 6 bug without the use of user-agent sniffing.
/**
* hasIOS6RequestAnimationFrameBug.
* @See {@Link https://gist.github.com/julienetie/86ac394ec41f1271ff0a} - Commentary.
* @Copyright 2015 - Julien Etienne.
* @License: MIT.
*/
function hasIOS6RequestAnimationFrameBug() {
var hasMobileDeviceWidth = screen.width <= 768 ? true : false,
requiresWebkitRequestAnimationFrame = !((window.webkitRequestAnimationFrame && window.requestAnimationFrame)),
hasNoNavigationTiming = window.performance ? false : true;