Skip to content

Instantly share code, notes, and snippets.

View sagiavinash's full-sized avatar

Sagi Avinash Varma sagiavinash

View GitHub Profile
@sagiavinash
sagiavinash / FunctionProperties.js
Created April 27, 2015 16:42
Function Properties (instead of global variables to store invokation values).
/*
instead of declaring global variables.
var counter = 0;
$(document).on("click", function(){
console.log(counter);
counter++;
});
*/
$(document).on("click", function handler(){
@sagiavinash
sagiavinash / getObjectClass.js
Created April 28, 2015 17:07
Get object class
function getClass(obj) {
if (typeof obj === "undefined")
return "undefined";
if (obj === null)
return "null";
return Object.prototype.toString.call(obj)
.match(/^\[object\s(.*)\]$/)[1];
}
getClass("") === "String";
@sagiavinash
sagiavinash / deepVal.js
Created April 28, 2015 17:08
Method to get value of nth level enumerable property of an object.
Object.defineProperty(Object.prototype, 'deepVal', {
enumerable: false,
configurable: false,
writable: false,
value: function (string){
var props = string.split("."),
val = {};
for(var key in this){
val[key] = this[key];
}
@sagiavinash
sagiavinash / jquery-importProp.js
Created April 28, 2015 17:10
Import object by values not references
;(function($){
$.importProp = function($this, $src, subset) {
if(subset){
$.each(subset, function(i, key){
$this[key] = $src[key];
});
} else {
$.each($src, function(key, e){
$this[key] = $src[key];
});
@sagiavinash
sagiavinash / JS_CallStackExecution_Demo.js
Created April 28, 2015 17:10
JS_CallStackExecution_Demo
console.log("1");
(function level1(){
console.log("2");
setTimeout(function async1(){
console.log("3");
},0);
(function level2(){
console.log("4");
setTimeout(function async2(){
console.log("5");
@sagiavinash
sagiavinash / efficientTypeConversion.js
Created April 28, 2015 17:11
Efficient Type Conversion
/* General Statement: To convert values to a particular datatype use type coercsion instead of explicit conversion. */
/* To Number */
var val = "123";
// 1. using Exclusing Type conversion.
console.log(Number(val));
// 2. using Type Coercion (unary plus operator)
console.log(+val);
// among these methods the Type Coercion is fast. (http://jsperf.com/number-vs-unary-plus)
@sagiavinash
sagiavinash / dataTypeCheck.js
Last active August 29, 2015 14:20
Check variable's primitive datatype
// just typepof myVar == 'string' fails when my var is declared => var myVar = new String("value");
if (typeof myVar == 'string' || myVar instanceof String){
// it's a string
}
// just typepof myVar == 'string' fails when my var is declared => var myVar = new String("value");
if (typeof myVar == 'number' || myVar instanceof Number){
// it's a string
}
@sagiavinash
sagiavinash / jquery-throttle.js
Created April 28, 2015 17:14
Throttle Snippet - Jquery
(function($) {
$.extend({
throttle : function(fn, timeout, ctx) {
var timer, args, needInvoke;
return function() {
args = arguments;
needInvoke = true;
ctx = ctx || this;
if(!timer) {
(function() {
@sagiavinash
sagiavinash / throttle.js
Created April 28, 2015 17:15
Throttle Snippet - Plain JS
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
@sagiavinash
sagiavinash / jquery-debounce.js
Created April 28, 2015 17:15
Debounce Snippet - Jquery
(function($) {
$.extend({
debounce : function(fn, timeout, invokeAsap, ctx) {
if(arguments.length == 3 && typeof invokeAsap != 'boolean') {
ctx = invokeAsap;
invokeAsap = false;
}
var timer;
return function() {
var args = arguments;