I am thinking that using object.prototype.stressing () for normal type testing with underlying types. Is reasonable. I have a function that looks like this:
// Return the underlying type of an object var typeOf = (function () {var reType = / [[Object (\ w +)] /; Return function type OFF (OBJ) {Type Return .XAC (Object Prototype. Tasting Call (OBJ)) [1];};}) (); On the
function, the call returns the following result:
console.log (typeOf (null)); // = & gt; Null console.log (typeOf (undefined)); // = & gt; Undefined console.log (typeOf ([])); // = & gt; Array console.log (typeOf (true)); // = & gt; Boolean Console Log (type (new date ())); // = & gt; Date console.log (typeOf (new error ())); // = & gt; Error console.log (typeOf (function () {})); // = & gt; Function Console Logs (type off (1)); // = & gt; Number console.log (typeOf ({}); // = & gt; Object console. Logs (type off (/ /)); // = & gt; RegExp console.log (typeOf ("")); // = & gt; String
Is this acceptable besides this fact that it can be slower than other types of investigation?
I am asking that there is a reason because I want to encode and sort the underlying type of an object for a project. I am working on i. I returned the type at a function Send a code that returns a numeric code:
// Encoded an underlying type of code as a number var encodeType = (function () {var types = {'null' : 0, 'Undefined': 1, 'Array': 2, 'Boolean': 3, 'Date': 4, 'Error': 5, 'f 'Function', 'number': 7, 'object': 8, 'regxp': 9, 'string': 10, 'logic': 11, 'monastery': 12, 'jesonon': 13}; type function encode (Type) {return type [type];}}) ();
then outputs:
console.log (type in sign (type (tap)); // = & gt; 0 console.log (type sign (typed (undefined))); // = & gt; 1 console.log (type in sign (typing ([]))); // = & gt; 2 console.log (encoded type (type (true))); // = & gt; 3 console.log (Type sign type (new date ()))); // = & gt; 4 console.log (type sign in sign (new error ()))); // = & gt; 5 console.log (encoded type (typeOf (function () {}))); // = & gt; 6 console.log (type in sign (type (1))); // = & gt; 7 console.log (type sign (typeOf ({})); // = & gt; 8 console.log (encoded type (type (/ /))); // = & gt; 9 console.log (type in sign (type (""))); // = & gt; 10
Is there any harm with this kind of investigation? Thanks for any insights.
Here's the implementation:
// Add some isType methods: IsArguments, isFunction, isString, isNumber, isDate, isRegExp, isError. _.each (['Argument', 'function', 'string', 'number', 'date', 'regex', 'error'], function (name) {_ ['is' + name] = function Obj) {return toString.call (obj) === '[object' + name + ']';};});
So yes, this method is good to go.
PS toString
in the above code is less for Object.prototype.toString
.
Comments
Post a Comment