JavaScript: what does Object (variable) do?

I came across a boolean test variable === Object(variable)

but couldn't find anything that describes it.

Does it check what is the variable

same as Object(variable)

and Object(variable)

pass this variable

to the object? Or is he doing something else?

If it matches it, there will be a loop for (var key in variable)

and uses key

and variable[key]

as parameters to another function. If it fails, it only uses that variable as is.

+3


source to share


1 answer


He checks that

  • variable defined
  • its value is an object
  • its value is not equal null

    (be careful: typeof null

    is "object"

    )

This is probably the easiest way to test these 3 conditions, and it looks like a sane test to be done before it loops on keys in a very polymorphic function.



It would be different typeof variable === "object" && variable

.

From MDN :

The object constructor creates an object wrapper for the given value. If the value is null or undefined, it will create and return an empty object, otherwise it will return an object of the type that matches the given value. If the value is already an object, it will return the value.

+3


source







All Articles