Created
February 14, 2023 13:35
-
-
Save DarrenSem/f7c667d544a31785f428e3ac73cd779f to your computer and use it in GitHub Desktop.
type.js AKA clazz constructor ctor.js (more granular than built-in typeof which returns "object" for most things)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// type.js AKA clazz constructor ctor.js (more granular than built-in typeof which returns "object" for most things) | |
// T=a=>({}).toString.call(a).slice(8,-1); // use case: if you have no need for JScript support | |
// T=function(a){return(null==a?a+"":{}.toString.call(a).slice(8,-1))}; // use case: non-JScript returns the same as JScript -- "null"/"undefined" instead of "Null"/"Undefined" | |
// T=function(a){return(/*@cc_on @if(@_jscript) null==a?a+"": @end@*/{}.toString.call(a).slice(8,-1))}; // use case: only JScript returns "null"/"undefined", while non-JScript returns "Null"/"Undefined" | |
var type = function(v) { | |
return ( | |
/*@cc_on @if(@_jscript) v == null ? String(v) : @end@*/ | |
{}.toString.call(v).slice(8,-1) // smaller than Object.prototype.toString.call(v).slice(8,-1) | |
); | |
}; | |
/*@cc_on @if(@_jscript) console={log:function(){var b=arguments;b.length&&WScript.Echo([].join.call(b," "))}}; @end@*/ | |
console.log([ | |
type({}), | |
type([]), | |
type(3.690), | |
type(Math.PI), | |
type(Infinity), | |
type(NaN), | |
type(), | |
type(undefined), | |
type([,][0]), | |
type(null) | |
]); | |
// (10) ['Object', 'Array', 'Number', 'Number', 'Number', 'Number', 'Undefined', 'Undefined', 'Undefined', 'Null'] | |
// JSCRIPT=['Object', 'Array', 'Number', 'Number', 'Number', 'Number', 'undefined', 'undefined', 'undefined', 'null'] | |
console.log([ | |
type(false), | |
type(""), | |
type(new Date), | |
type(Date), | |
type(Error), | |
type(new Error("42")), | |
type(new TypeError("_")), | |
type(/xyz/img) | |
]); | |
// (8) ['Boolean', 'String', 'Date', 'Function', 'Function', 'Error', 'Error', 'RegExp'] | |
if(Date.now)console.log([ | |
type(location), | |
type(new WeakMap()), | |
type(Symbol(42)), | |
type(BigInt(9001)), | |
type(new Float64Array( [ 3.14, 0.15, 0.9] ) ), | |
type(eval("var gen = function*(){}; gen")), | |
type(eval("var af = async function(){}; af")), | |
type(eval("class cls{}; cls")), | |
type(eval("class CustomArray extends Array { }; new CustomArray()")), // via https://stackoverflow.com/questions/62074163/understanding-array-isarray-polyfill/62075462#62075462 | |
type(eval("class Custom { }; new Custom()")), | |
type(eval("var arw = () => {}; arw")), | |
type(document.querySelectorAll("*")) | |
]); | |
// (12) ['Location', 'WeakMap', 'Symbol', 'BigInt', 'Float64Array', 'GeneratorFunction', 'AsyncFunction', 'Function', 'Array', 'Object', 'Function', 'NodeList'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment