Execute tostring () forcibly in Javascript ()

Source: Internet
Author: User

Original article: enforcing tostring ()

Translation: Javascript
Tostring ()

Translator: singleseeker

Javascript will automatically convert the value to the desired type based on the needs of methods or operators, which may lead to various errors. Brian McKenna (@ puffnfresh) suggests provides the following test code:

Object.prototype.valueOf = function () {    throw new Error('Use an explicit toString');};

What are the effects of these codes? You can no longer use the plus sign operator to convert an object into a string:

> var obj = {};> 'Hello '+objError: Use an explicit toString> String(obj)'[object Object]'> obj.toString()'[object Object]'> 'Hello '+String(obj)'Hello [object Object]'

What is this? To convert an object to a specific basic type T, first, its value is converted to a basic type, and then to T. The previous conversion is completed in two steps:

  1. CallvalueOf()Method. If a basic type is returned, it ends.

  2. Otherwise, call the MethodtoString(). If a basic type is returned, end

  3. Otherwise, an error is thrown.

If the final conversion is a numeric value, it is calledvalueOf()AndtoString().

If the final conversion is a stringtoStringWill be called first. The plus sign operator may be converted into a numeric or string type, but it usually produces a basic type based on numeric operations.

Code snippets that do not need to be sent at the beginning of the article,Object.prototype.valueOf()The object itself will be returned. This is a method that continues from the native object without being overwritten:

> var obj = {};> obj.valueOf() === objtrue

The plus sign operator will eventually calltoString(). The code snippet above blocks the call and throws an error before the method can be called.

Note that this error message is not always correct.

> Number(obj)Error: Use an explicit toString

However, this trick is useful.

If an object really wants to be converted into a number, it still needs to call its ownvalueOfMethod.

@ Singleseeker: I really want to translate this article, but it is good to summarize the knowledge points, however, as an English Technical article written by a foreigner who is not a mother-tongue English language, I am a cainiao translator who is not a mother-tongue English.
The following is a summary.

  1. UsuallyvaluOf()Indicates that an unconverted object is returned, that is, its own

  2. In additionDateAlmost all objects are called first.valueOf()Method

  3. IfvalueOf()Returns a clear basic value type. when an object is added to a string,toString()Will not be called

Reference
  1. Forced conversion object (objects) to original value (primitives)

  2. In JavaScript, how much is {} + {} equal?

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.