Home > Web Front-end > JS Tutorial > body text

Specific implementation of toString() forced execution in javascript_javascript skills

WBOY
Release: 2016-05-16 17:35:14
Original
1507 people have browsed it

Original text: Enforcing toString()
Translation: Enforcing toString() in javascript
Translator: singleseeker
Javascript usually follows Methods or operators are required to automatically convert the value to the required type, which can lead to various errors. Brian McKenna (@puffnfresh) suggests provided the following test code:

Copy code The code is as follows:

Object.prototype.valueOf = function () {
throw new Error('Use an explicit toString');
};
[sS ]* n
What effect will these codes have? You can no longer use the plus operator to convert an object to a string:
[code]
> var obj = {};
> 'Hello ' obj

Error: Use an explicit toString
Copy code The code is as follows:

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

What's going on with 'Hello [object Object]'? To convert an object to a specific basic type T, first its value is converted to the basic type, and then converted to T. The previous conversion is completed in two steps: 1. Call the valueOf() method. If it returns a basic type, then end 2. Otherwise, call the method toString(). If a basic type is returned, then end
3. Otherwise, throw an error
If the last conversion is a numeric value, it will be the above order of calling valueOf() and toString.
If the last conversion is a string, then toString will be called first. The plus operator may convert a value to a numeric or string type, but it
usually produces a primitive type
based on numeric operations. Instead of the code snippet posted at the beginning of the article, Object.prototype.valueOf() will return the object itself. This is an unoverridden method inherited from the native object:

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


true The plus operator will eventually call toString(). The above code snippet blocks the call and throws an error before the method can be called.
Note that this error message is not always completely correct.


> Number(obj)


Error: Use an explicit toString But this trick is definitely useful.
If an object really wants to be converted to a number, it has to call its own valueOf method anyway.

@singleseeker
Wordy: This article is translated to make me more complaining, and the knowledge points are summarized well. However, as an English technical article written by a foreigner who is not a native English speaker, it is handed over to me. As a novice translator whose native language is not English, it is really torturous. A brief summary follows. 1. Usually valuOf() indicates to return an unconverted object, which is itself 2. Except for the Date object, the plus operator almost always calls the valueof() method first
3. If Make valueof() return a clear basic numerical type, then when an object is added to a string, toString() will not be called
Reference
1.
Forcing objects (objects) to Primitives
2.In JavaScript, what is {} {} equal to?
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template