[object, Object] in js

I typed a piece of code today, but I made an error when I wrote it:
Object.prototype.toString.call (prop) ="[object Object]"/ / the code in the book
when I wrote it myself, I wrote"[object Object]"as"[Object Object]"/ / both object capitalized

.

what do these two object stand for?

about toString (), this method is based on the prototype of Objet, and prop is an object, so use call (),. Why don"t I just prop.toString ()?

in addition:
{} .toString () / / error
var obj = {};
obj.toString (); / / "[object Object]"
Why is this the case?

May.08,2021

what do these two object stand for?
by default, the, toString () method is inherited by each Object object. If this method is not overridden in a custom object, toString () returns "[object type]", where type is the type of object.

Why don't I just prop.toString ()?
this code Object.prototype.toString.call (prop) is used to determine the type. No matter what type prop is, the return must be "[object type]", but prop is not necessarily a type returned by Object,. For example, 1.toString () returns an error, and 'aaa'.toString () returns' aaa'

.

{} .toString () / / error
is a syntax error. The js engine that begins with curly braces will think it is a code block, so it will report an error. Just change it to ({}). ToString ())


what do these two object stand for?

object tells you that the base type is object , but object contains many subtypes such as Array/Boolean , while the second Object tells you which subtype it belongs to.

Why don't I just prop.toString () like this?

because prop is an uncertain thing, is a parameter, and may be a base type, there is no toString , so use Object.prototype.toString.call to avoid reporting errors.

{} .toString () / / error

{} .toString () the error is reported because {} is parsed into a code block, and you should change it to ({}). ToString ()

).

https://developer.mozilla.org.
https://tc39.github.io/ecma26.
(19.1.3.6)
Object.prototype.toString is specified in the standard.
you can customize the latter Object

according to the standard.
function Foo(){}
Foo.prototype[Symbol.toStringTag]='Foo'
Object.prototype.toString.call(new Foo)// "[object Foo]"
var Bar={[Symbol.toStringTag]:'Bar'}
Object.prototype.toString.call(Bar)// "[object Bar]"
< hr >
{}.toString() 

this error is because {} is considered to be a code block rather than an object

Menu