Skip to content

Instantly share code, notes, and snippets.

View sagiavinash's full-sized avatar

Sagi Avinash Varma sagiavinash

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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(){