Cycymomo
4/22/2013 - 3:43 PM

isTypeX.js

Object.prototype.getType = function() {
  return typeof this;
};
Object.prototype.getTypeStrict = function() {
  "use strict";
  return typeof this;
};
Object.prototype.getInstance = function() {
  return 'instanceof String : ' + (this.valueOf() instanceof String) + ' / instanceof Object : ' + (this.valueOf() instanceof Object);
};
Object.prototype.getInstanceStrict = function() {
  "use strict";
  return 'instanceof String : ' + (this.valueOf() instanceof String) + ' / instanceof Object : ' + (this.valueOf() instanceof Object);
};
console.log('Type (no strict) : ' + "".getType()); // object
console.log('Type (stric) : ' + "".getTypeStrict()); // string
console.log('Instance (no strict) : ' + "".getInstance()); // object
console.log('Instance (strict) : ' + "".getInstanceStrict()); // string
function isMap(m) { //similarly for Set, WeakMap, WeakSet
   try {
      Map.prototype.size.call(m);
      return true;
   } catch () {return false}
}

function isDataView(d) {
   try {
      DataView.prototype.buffer.call(d);
      return true;
    } catch () {return false}
}

function isPromise(p) {
   try {
      Promise.prototype.then.call(p);
      return true;
    } catch () {return false}
}

function isTypedArray(a) {  //any TypedArray instance
   try {
      Uin32Array.prototype.buffer.call(a);
      return true;
    catch () {return false}

function isUint8ArrayArray(a) {  //for exmaple
   try {
      return Uin8Array.prototype[Symbol.toStringTag].call(a)==="Uint8Array"; ? true : false;
    } catch () {return false}
}

function isDate(d) {  //for exmaple
   try {
      Date.prototype.getDay.call(d);
      return true;
    } catch () {return false}
}