JavaScript invisible type conversion

This chapter is focused when I read The Definitive Guide JavaScript attention to content, but also recommended are learning the front of a small partner can look at the book "The Definitive Guide JavaScript"

 

 

JavaScript can be very flexible to convert the value of one type to another type. When we use the string in the target environment (the string as an attempt to target the same to visit his method or property), JavaScript will think that this string value to create a String wrapper object internally

E.g:

     var str = 'hello word'

     console.log(str.length)

    // implicit packaged console.log (new String ( 'hello work'). Length)

When we use the string in the target environment, pay attention String object is created is only transient existence, he allows us to access the properties and methods, then there is no use, so it'll be dropped

var s = 'hello wold' // original string

var S = new String ( 'hello wold') // String object

After you create a String object S, what can we do with it? In fact, what we can do with the original string value, what you can do to get the String object, except when we use the typeof can not find them in addition to the same type, no other difference. We have already seen the string will be automatically converted to an object when it is necessary, the results proved that the result of the conversion and the original string is no different.

When we need to use a raw string and an object, the object will be automatically converted to a string:

E.g:

    var str = new String('hello word')

    was STR = str + ''

  

The above process is an example of a transient triggered transient String object to perform the connecting operation of the original string

 

All involved in this chapter of String objects involved are suitable for Number, Boolean

 

Guess you like

Origin www.cnblogs.com/tffan/p/12080007.html