Forcing ToString () in JavaScript to implement _javascript techniques

Source: Internet
Author: User
Original: Enforcing toString ()
Forced ToString () in JavaScript
Translator: Singleseeker
JavaScript usually automatically turns values into the desired type based on the needs of the method or operator, which can cause various errors. Brian McKenna (@puffnfresh) suggests provides the following test code:
Copy Code code as follows:

Object.prototype.valueOf = function () {
throw new Error (' use a explicit toString ');
};
[\s\s]*\n
What's the effect of this code? You can no longer use the plus operator to turn a pair like a string:
[Code]
> var obj = {};
> ' Hello ' +obj

Error:use an explicit toString
Copy Code code as follows:

> String (obj)
' [Object Object] '
> obj.tostring ()
' [Object Object] '
> ' Hello ' +string (obj)

' Hello [Object Object] ' What's this about? To turn an object into a specific basic type T, first of all, its value is converted to the basic type, then the transformation to T, the previous conversion is done in two steps:
1. Call the ValueOf () method, if return a base type, then end
2. Otherwise, call Method ToString (). If you return a base type, the end
3. Otherwise, throw the error
If the final conversion is a numeric value, it will be the order of the above call valueof () and toString.
If the final conversion is a string, then toString is invoked first. The plus operator may be converted to a numeric or string type, but it usually produces a basic type based on numeric operations.
Instead of the code snippet that starts at the beginning of the article, Object.prototype.valueOf () returns the object itself, a method that continues from the native object without being rewritten:
Copy Code code as follows:

> var obj = {};
> obj.valueof () = = obj

The true plus operator will eventually call ToString (). The code fragment above blocks the call and throws an error before the method can be invoked.
Note that this error message is not always completely correct.
Copy Code code as follows:

> Number (obj)

Error:use an explicit ToString but it is useful to throw this trick.
If an object really wants to be transformed into a number, it still has to invoke its own valueof method anyway.
@singleseeker Wordy: This article translated really want to more kind of spit groove, knowledge points summed up is good, but as a not English-speaking foreigner written in English technical articles to me a native language is not English rookie translation, indeed enough to torture people. Here's a quick summary.
1. Usually Valuof () indicates the return of an object that is not converted, which is itself
2. The plus operator is almost entirely called the valueof () method except the Date object
3. If valueof () returns an explicit basic numeric type, toString () will not be invoked when an object is added to the string.
Reference
1. Cast object (objects) is the original value (primitives)
2.JavaScript, {}+{} equals how much?

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.