Forced type conversions for JavaScript object oject

Source: Internet
Author: User
Tags object object

JavaScript is well known as a dynamic type, and the type conversion occurs in many cases by the weak type of the scripting language whose data type. These types of conversions are often implicit, which gives us a lot of trouble when using JS. And JS's basic data type conversion is not too much elaborated in this article, the main record JS object data type conversion. The author because of comparative dishes, if there is no wrong to write the place welcome the big guy in the bottom of the message.

1.Number Cast Object
Let a={name:123};console.log (number (a));

We first simply declare an object and use number () to force the type conversion

The results of the operation are as follows:

So let's analyze why the object is forced into a type conversion.

Here you have to mention the JS object with two methods, valueof and ToString.

Let's talk about the valueof method first:

The JavaScript call valueOf method converts the object to the original value. You seldom need to call the valueOf method yourself, and JavaScript will call it automatically when you encounter an object that expects the original value.

By default, the valueOf method is Object inherited by each subsequent object. Each built-in core object overrides this method to return the appropriate value. If the object has no original value, the valueOf object itself is returned.

Many of the built-in objects of JavaScript rewrite the function to make it more suitable for its own functional needs. Therefore, the return and return value types of the valueof () method for different types of objects may be different.

The above description derives from the description of the valueof method from the MDN, and the return values for the different data types valueof are given below:

Note that the return value above is the return value if you do not have a function that overwrites the original valueof.

In the table above we can see clearly that the value returned by the object is the object itself, the result is as follows:

Now let's talk about the ToString method

The return value for an object that does not overwrite the ToString method should be:

So what is the relationship between these two approaches and the coercion type conversion of an object?

In fact, when the number () coercion type converts the object, the following actions are performed:

1. Call the object's ValueOf method first

2. Determine if the return value of the method is the underlying data type (NUMBER,STRING,BOOLEAN,UNDEFINED,NULL)

3. If the return value is the underlying data type, the conversion rule converts it according to the conversion rules of the corresponding data type

4. If the return value is not the underlying data type, continue calling the ToString method on the basis of the return value

5. Determine if the return value of ToString is the underlying data type

6. Determine whether the underlying data type, if the underlying data type, operates 3

7. If the underlying data type is still not an error

Here we verify these steps:

This seems to explain the pass, then let's see if the return value of ToString is still an object to see if the browser will be error:

Sure enough, when the ToString return value is still the object, JS error

So we're going to verify what happens if the return value of the first call to valueof is the underlying data type?

So the procedure for number casting of objects is as above 7 steps

2.String Cast Object

First of all, a simple object is still first declared.

In order to differentiate it from the above we create an object b{name:b}

From what we can see, the result of the cast is "[Object Object]"

The following is the decryption time:

In fact, the steps for a string cast object are similar to number, and are divided into similar 7 steps:

1. Call the object's ToString method first

2. Determine if the return value of the method is the underlying data type (NUMBER,STRING,BOOLEAN,UNDEFINED,NULL)

3. If the return value is the underlying data type, the conversion rule converts it according to the conversion rules of the corresponding data type

4. If the return value is not the underlying data type, continue calling the ValueOf method on the basis of the return value

5. Determine whether the return value of the valueof is the underlying data type

6. Determine whether the underlying data type, if the underlying data type, operates 3

7. If the underlying data type is still not an error

The difference between string and number is that

Number is called valueof before calling ToString

The string is called by ToString before calling valueof

For convenience, this is illustrated in a single diagram:

So that's how the string transforms the object.

3.Boolean Cast Object

We know that there are only 5 values in JS that can be false at the time of judgment.

1.undefined

2.null

3. '//empty string

4.0

5.NaN

The other values are true at the time of the judgment, and if is determined by using a Boolean to convert the

So the value of the Boolean conversion of the object is True

——————————————————————————————————————————————————————————————————

So the answers to some interesting interview questions are explained:

{}+{}

{}+[]

[]+[]

[]+{}

Related Article

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.