Skip to content

Instantly share code, notes, and snippets.

@ironboy
ironboy / simpleObjectCreateExtend.js
Created February 29, 2016 18:05
Supershort version of Object.create - extend pattern
// The supershort version
// with no polyfills or extras (as super, named constructors etc.)
// And nice way to handle inheritance is by creating a base object
// with an extend method
var Base = {
extend: function(props){
// A new object with this object as its prototype
var obj = Object.create(this);
// Assign properties to the new object
@ironboy
ironboy / objCreateExplored.js
Last active February 29, 2016 17:37
ES5 inheritance using Object.create and similar patterns
// First things first - polyfills for IE
// Thomas' minimal Object.assign polyfill
Object.assign = Object.assign || function(){
for(var i = 1; i < arguments.length; i++){
for(var j in arguments[i] || {}){
arguments[0][j] = arguments[i][j];
}
}
};
@ironboy
ironboy / propSearch.js
Last active February 1, 2016 16:22
Search JS data structures with ease
var propSearch = (function(){
/*
ironboy, Jan 2016
Search an endlessly deep/complex JS data structure with ease
Call this function with a search object as your input parameter.
searchObj:
// A cool JSON reviver
// Thomas Frank 2015
// MIT licensed
JSON.cool = (function(){
var mem, findMem, base, toFind, settings = {
circulars: true,
dates: true,
functions: false,
@ironboy
ironboy / Object.getPrototypeOfPolyFill.js
Created November 5, 2015 10:48
john resigs polyfill for Object.getPrototypeOf
if ( typeof Object.getPrototypeOf !== "function" ) {
if ( typeof "test".__proto__ === "object" ) {
Object.getPrototypeOf = function(object){
return object.__proto__;
};
} else {
Object.getPrototypeOf = function(object){
// May break if the constructor has been tampered with
return object.constructor.prototype;
};
Object.slice = function(){
var
args = [].slice.call(arguments),
obj = args.shift(),
obj2 = {},
keys = Object.keys(obj);
keys.slice.apply(keys,args).forEach(function(x){
obj2[x] = obj[x];
});
return obj2;
Number.prototype.slice = function(x,y){
return (this+'').slice(x,y)/1;
}
$scope.changeWatch = function(){
// An alternative to Angular $scope.$watch
// that does not trigger on initial load
var
init = true,
args = [].slice.call(arguments),
listener = args[1];
// options object example
/*
{
decimals:2,
decimalSign:",",
thousandSep: " ",
currency:"SEK"
}
*/
@ironboy
ironboy / date format.js
Last active July 27, 2016 12:41
date format
// Example usage niceFormat
// new Date().niceFormat('yyyy-mm-dd hh:ii:ss')
// Example usage countdown
// new Date().countdown(dateInFuture,'ii:ss')
// try with new Date().countdown(new Date(new Date().getTime()+3600000),"ii:ss")
(function(){
var a = {