JS exactly determine why the data type is using Object.prototype.toString.call () instead of Object.prototype.toString ()

Source: Internet
Author: User

What is the difference, why make sure to go through call.

We know that call is used to change the scope of the function, and Object.prototype.toString.call is used here to change the scope.

Object.prototype.toString () When the ToString method is called, performs the following steps (refer to):

1. Gets the value of the [[Class]] property of this object.

2. Calculated three string "[object", the first step of the operation result (1), and "]" after the connection of the new string.

3. Returns the result of the operation of the second step (2).

[[Class]] is an intrinsic property, and all objects (native and host objects) have that property. In the specification, [[class]] is defined: The internal property describes a string value that indicates the type of the object.

The process is simple: 1. Gets the class name (object type) of the object. 2. Then combine [object, acquired class name,] and return.

The key place to use call is in the 1th step, get the This object, no call change scope when this point is window Ah!

Also, why do we have to use the top-most object.prototype of the prototype chain, rather than just using the data type object, because when I find the ToString method on the prototype chain, I don't find the object.

, the ToString method of its own object has been found, and this method has rewritten object.

JS exactly determine why the data type is using Object.prototype.toString.call () instead of Object.prototype.toString ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.