Execute toString () forcibly in javascript _ javascript skills

Source: Internet
Author: User
Javascript will automatically convert the value to the required type based on the needs of methods or operators, which may lead to various errors. Next we will introduce how to force the execution of toString () in javascript (), if you are interested, refer to the original article: Enforcing toString ()
Translate: Execute toString () forcibly in javascript ()
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:

The Code is as follows:


Object. prototype. valueOf = function (){
Throw new Error ('use an explicit tostring ');
};
[\ S \ S] * \ n
What are the effects of these codes? You can no longer use the plus sign operator to convert an object into a string:
[Code]
> Var obj = {};
> 'Hello' + obj


Error: Use an explicit toString

The Code is as follows:


> String (obj)
'[Object Object]'
> Obj. toString ()
'[Object Object]'
> 'Hello' + String (obj)


'Hello [object] 'what is the problem? 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. Call the valueOf () method. If a basic type is returned, it will end.
2. Otherwise, call the toString () method (). If a basic type is returned, end
3. Otherwise, an error is thrown.
If the final conversion is a numerical value, it is the order in which valueOf () and toString are called above.
If the final conversion is a string, the toString will 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.
Object. prototype. valueOf () will return the Object itself, which is a method not rewritten from the native Object:

The Code is as follows:


> Var obj = {};
> Obj. valueOf () = obj


The true + operator will eventually call toString (). 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.

The Code is as follows:


> Number (obj)


Error: Use an explicit toString, but this trick is useful.
If an object really wants to be converted into a number, it still needs to call its own valueOf method in any case.
@ 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. Usually valuOf () indicates that an unconverted object is returned, that is, its own
2. Except for Date objects, the plus sign operator calls the valueof () method first.
3. If valueof () returns a clear basic value type, toString () will not be called when an object is added to a string.
Reference
1. Force convert the object (objects) to the original value (primitives)
2. What is the value of {} + {} In JavaScript?

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.