【JavaScript】——"Re-learning front-end" 01 data type

        We must realize that 3 and new Number(3) are completely different values, one is of type Number and the other is of type Object .
Number, String and Boolean, the three constructors are dual-purpose, when used with new, they generate objects , and when called directly, they represent mandatory type conversion .

The Symbol function is special, calling it directly with new will throw an error, but it is still the constructor of the Symbol object.

In many practices, it is recommended to prohibit the use of "==" , and require programmers to use === to compare after explicit type conversion.

Type conversion from string to number: 

        If the second parameter is not passed in, parseInt only supports the hexadecimal prefix "0x" , and ignores non-numeric
characters, and does not support scientific notation.

        In some ancient browser environments, parseInt also supports numbers starting with 0 as octal prefixes, which is the source of many errors. So in any environment, it is recommended to pass in the second parameter of parseInt .

        And parseFloat directly parses the original string as decimal , it will not introduce any other base.

        Number is a better choice than parseInt and parseFloat in most cases.

Boxing conversion:

        Each basic type Number, String, Boolean, and Symbol has a corresponding class in the object. The so-called boxing conversion is to convert the basic type into the corresponding object . It is a very important type in type conversion.

        The global Symbol function cannot be called with new, but we can still use the boxing mechanism to get a Symbol object, and we can use the call method of a function to force boxing.

//我们定义一个函数,函数里面只有 return this.
//然后我们调用函数的 call 方法到一个Symbol 类型的值上.
//这样就会产生一个 symbolObject。 
var symbolObject = (function(){ return this; }).call(Symbol("a"));
 console.log(typeof symbolObject); //object
 console.log(symbolObject instanceof Symbol); //true
 console.log(symbolObject.constructor == Symbol); //true

        The boxing mechanism will frequently generate temporary objects. In some scenarios with high performance requirements, we should try to avoid boxing conversions for basic types.

        Using the built-in Object functions , we can explicitly invoke boxing capabilities in JavaScript code.

 var symbolObject = Object(Symbol("a"));
 console.log(typeof symbolObject); //object
 console.log(symbolObject instanceof Symbol); //true
 console.log(symbolObject.constructor == Symbol); //true

In JavaScript, there is no way to change the private Class property, so
Object.prototype.toString is a method that can accurately identify the basic type corresponding to the object, which is
more accurate than instanceof.
But it should be noted that call itself will generate a boxing operation , so it needs to cooperate with typeof to distinguish between basic types and
object types.

Unboxing conversion:

The unboxing conversion will attempt to call valueOf and toString to obtain the unboxed primitive type. If neither valueOf nor
toString is present, or if no primitive type is returned, a TypeError will be raised.

        When performing the operation of o*2, you will see that valueOf is executed first, followed by toString, and finally a TypeError is thrown, which means that the unboxing conversion failed.

 var o = {
     valueOf : () => {console.log("valueOf"); return {}},
     toString : () => {console.log("toString"); return {}}
 }

 o * 2
 // valueOf
 // toString
 // TypeError

        An unboxing conversion to String prefers calling toString. We changed the previous operation from o*2 to String(o), then
you will see that the order of calls has changed.

 var o = {
     valueOf : () => {console.log("valueOf"); return {}},
     toString : () => {console.log("toString"); return {}}
 }
 String(o)
 // toString
 // valueOf
 // TypeError

Practice questions:

If we don't use native Number and parseInt, but use JS code to convert String to Number, what should we do?

let StringToNumber = (str)=>{
    let arr = str.trim().split('')
    let sign = arr[0] === '-' ? -1 : 1 //sign标记符号
    if (sign === -1 || str[0] === '+') {  //如果有符号就删除
        arr.shift()
    }
    return sign * arr.reduce((total, cur)=>(
        total * 10 + (cur >= '0' && cur <= '9' ? (cur - '0') : NaN)
    ))
}

Guess you like

Origin blog.csdn.net/qq_50497708/article/details/128171028