Friday, May 12, 2017

Dual not operator

var obj = "OK";
var match = obj.match(/ERROR/);
console.log(match);
console.log(!!match);
Output:
null
false

Typeof

var typeOfArray = {
 each: function( obj, callback ) {
  var i = 0, length = obj.length;
  console.log("obj[" + length + "]=>" + obj);
  for ( ; i < length; i++ ) {
   console.log(obj[i])
   callback.call(this, obj[ i ]);
  }
 }
}
var class2type = {};
var toString = class2type.toString;
typeOfArray.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ),
function(name) {
 class2type[ "[object " + name + "]" ] = name.toLowerCase();
} );
function toType(x) {
 if (x == null ) {
  return x + "";
 }
    return toString.call(x);
}
function type(x) { 
    console.log("typeof " + x + "=" + typeof x);
    console.log("Object.toString " + toType(x));
    console.log("Object type " + class2type[toType(x)]);
}
var n;
type(n);
var x = null;
type(x);
type(2);
type("2");
type("xyz");
type(true);
type({x: 1});
type(2.2);
type(["x", "y"]);
type(function(){});
type (new Boolean(true));
type(new Array(2,3));
type(new Number(2));
type(new Date());
type(new RegExp("[a-b]"));
type(new String("Hi"));
Output:
obj[10]=>Boolean,Number,String,Function,Array,Date,RegExp,Object,Error,Symbol
typeof:6 Boolean
typeof:6 Number
typeof:6 String
typeof:6 Function
typeof:6 Array
typeof:6 Date
typeof:6 RegExp
typeof:6 Object
typeof:6 Error
typeof:6 Symbol
typeof:24 typeof undefined=undefined
typeof:25 Object.toString undefined
typeof:26 Object type undefined
typeof:24 typeof null=object
typeof:25 Object.toString null
typeof:26 Object type undefined
typeof:24 typeof 2=number
typeof:25 Object.toString [object Number]
typeof:26 Object type number
typeof:24 typeof 2=string
typeof:25 Object.toString [object String]
typeof:26 Object type string
typeof:24 typeof xyz=string
typeof:25 Object.toString [object String]
typeof:26 Object type string
typeof:24 typeof true=boolean
typeof:25 Object.toString [object Boolean]
typeof:26 Object type boolean
typeof:24 typeof [object Object]=object
typeof:25 Object.toString [object Object]
typeof:26 Object type object
typeof:24 typeof 2.2=number
typeof:25 Object.toString [object Number]
typeof:26 Object type number
typeof:24 typeof x,y=object
typeof:25 Object.toString [object Array]
typeof:26 Object type array
typeof:24 typeof function (){}=function
typeof:25 Object.toString [object Function]
typeof:26 Object type function
typeof:24 typeof true=object
typeof:25 Object.toString [object Boolean]
typeof:26 Object type boolean
typeof:24 typeof 2,3=object
typeof:25 Object.toString [object Array]
typeof:26 Object type array
typeof:24 typeof 2=object
typeof:25 Object.toString [object Number]
typeof:26 Object type number
typeof:24 typeof Fri May 12 2017 20:45:31 GMT+0530 (India Standard Time)=object
typeof:25 Object.toString [object Date]
typeof:26 Object type date
typeof:24 typeof /[a-b]/=object
typeof:25 Object.toString [object RegExp]
typeof:26 Object type regexp
typeof:24 typeof Hi=object
typeof:25 Object.toString [object String]
typeof:26 Object type string
typeof:1 undefined

Thursday, May 11, 2017

Data types

function type(x) {
    console.log("typeof " + x + "=" + typeof x);
}
var n;
type(n);
var x = null;
type(x);
type(2);
type("2");
type("xyz");
type(true);
type({x: 1});
type(2.2);
type(["x", "y"]);
type(function(){});
type (new Boolean(true));
type(new Array(2,3));
type(new Number(2));
type(new Date());
type(new RegExp("[a-b]"));
type(new String("Hi"));
Output:
typeof undefined=undefined
typeof null=object
typeof 2=number
typeof 2=string
typeof xyz=string
typeof true=boolean
typeof [object Object]=object
typeof 2.2=number
typeof x,y=object
typeof function (){}=function
typeof true=object
typeof 2,3=object
typeof 2=object
typeof Fri May 12 2017 13:56:47 GMT+0530 (India Standard Time)=object
typeof /[a-b]/=object
typeof Hi=object

split

var values = "a b c d".split(" ");
console.log("values: " + values);
var i = 0;
for (i in values) {
    console.log("values[" + i + "]: " + values[i]);
}
Output:
values: a,b,c,d
values[0]: a
values[1]: b
values[2]: c
values[3]: d

Extend an Object

var o = function() {
}
o.extend = function() {
    var args = arguments[0];
    if (args != null) {
        for (arg in args) {
            this[arg] = args[arg];
        }
    }
}

o.extend({x: "I am x", y: "I am y"});

console.log(o.x);
console.log(o.y);
Output:
I am x
I am y

Traverse thru object arguments

var x = function() {
    var args = arguments[0];
    for (argName in args) {
        console.log("argument: " + argName);
        console.log("value: " + args[argName]);
    }    
}
x({y: "This is y",
   z: "This is z"});
Output:
argument: y
value: This is y
argument: z
value: This is z

Wednesday, May 10, 2017

regex

rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, // Matches dashed string for camelizing rmsPrefix = /^-ms-/, rdashAlpha = /-([a-z])/g,

Array push


Array concat


Array slice


Object.getPrototypeOf


Passing function as argument

( function( global, factory ) {

 "use strict";

 if ( typeof module === "object" && typeof module.exports === "object" ) {
  module.exports = global.document ?
   factory( global, true ) :
   function( w ) {
    if ( !w.document ) {
     throw new Error( "jQuery requires a window with a document" );
    }
    return factory( w );
   };
 } else {
  factory( global );
 }

// Pass this if window is not defined yet
} )( typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
});

"use strict"


Monday, May 8, 2017

Shortcut name of function

var ThisIsALongVar = function() {

    var svar = {
        config: {
            alertPrefix: 'ShortcutVar Example'
        },        
        say: function(param) {
            return alert(param);
        }
    }

    function alert(str) {
	    window.alert(svar.config.alertPrefix + str);
    }
    
    return svar;
}();

console.log(ThisIsALongVar.say("Hello"));
Output: