[object Window]
Object.prototype.toString.call(this) // '[object Window]'
Object.prototype.toString.call(window) // '[object Window]'
this === window // true
[object Object]
function stringify (x) {
console.log(Object.prototype.toString.call(x));
}
console.log(stringify({})); // console output: [object Object]
// typeof {} // 'object'
NOTE :
[object Object]
is most common one . Sometimes it happens then you e.g.:
console.log(`Begin ${object_variable} end.`)
: if variable of object_variable
returns object rather than primitive you will definitely end up into [object Object], solution would be console.log() it outside the template literals e.g.:
console.log(`Begin`, object_variable, `end.`)
: unless object_variable.some_property
whereas some_property
is some primitive such as Number or String literal then you could stick within template literals as in a first place shown above .
[object Array]
typeof [] /* 'object' : technically it's true as everything in JS is an object under the hood , although it limits debugging , a solution to this is : */
function stringify (x) {
console.log(Object.prototype.toString.call(x));
}
console.log(stringify([])); // console output: [object Array]
// alternatively do so :
Object.prototype.toString.call([]) === '[object Array]' // true
// or simply :
Array.isArray([]) // true
NOTE : we use Array.prototype quite often , sometimes we need to check type i.e. double check if it's not an object an we can apply Array.prototype otherwise if e.g.
.push()
applied upon Object.prototype would end up in an error message of ".push() is not a function" – common sense – you cannot expect otherwise as Object.prototype is not ant Array i.e. it has no idea what a heck is.push()
& what it is doing in a wrong world of Object's ..!
[object String]
// Hereby string presented as object rather than primitive !
Object.prototype.toString.call("" || '' || ``); // [object String]
// e.g.:
Object.prototype.toString.call("" || '' || ``) !== Object.prototype.toString.call(RegExp); // true
// although be careful :
String.prototype.toString.call("food"); // "food"
Object.prototype.toString.call("food"); // [object String]
I guess least used option would be a typeof 'function' , i.e.:
[object Function]
// in function declaration form :..
Object.prototype.toString.call(function(){}); // '[object Function]'
typeof function(){} // 'function'
// in function factory form :..
typeof Function // 'function'
Object.prototype.toString.call(Function); // '[object Function]'
// although be careful as some X.prototype may still be returned as Function if CCCapitalized e.g.:
Object.prototype.toString.call(Object || Array || String || RegExp); '[object Function]'
To conclude : in most of programming langs the most important is to know what type is returned (debugging) or expected to be returned (testing) – then you know the type i.e. the X.prototype you are dealing with – worst is in the past . Now jump in your IDE & do some coding as well as little of debugging cause it's fun !
Top comments (0)