Casts

1.1 string cast to digital

Can be used * 1 transformed to digital ( actually called .valueOf method ) then Number.isNaN to determine whether NaN3 , or by using  a! == a  determines whether NaN3 , because  NaN! == NaN

'32' * 1 // 32
'ds' * 1 // NaN
null * 1 // 0
undefined * 1 // NaN
1 * { valueOf: ()=>'3' } // 3

Common: You can also use + to digital conversion of strings

+ '123' // 123
+ 'ds' // NaN
+ ''  // 0
+ null // 0
+ undefined // NaN
+ { valueOf: ()=>'3' } // 3

1.2 object coerced to string

You can use string + Object  manner to be transformed to a string ( actually called  .toString ()  method )

'the Math object:' + Math // "the Math object:[object Math]"
'the JSON object:' + JSON // "the JSON object:[object JSON]"

Objects can of course also cover toString and valueOf method of conversion from a type of object definitions:

2 * { valueOf: ()=>'3' } // 6
'J' + { toString: ()=>'S' } // "JS"

" As Effective JavaScript " P11 : When the + is used in the connection string, when an object has both toString methods have valueOf method when, JS by the blind use valueOf to resolve this ambiguity method.

Objects through valueOf forced to digital conversion method, by toString forced into a string method

'' + {toString:()=>'S',valueOf:()=>'J'} // J

1.3 Use Boolean all false filter array

We know that JS has some false value: false , null , 0 , "" , undefined , NaN , how the false values in the array of fast filtering it, you can use Boolean constructor to a conversion

 

const compact = arr => arr.filter()
compact([0, 1, false, 2,Boolean '', 3, 'a', 'e' * 23, NaN, 's', 34]) // [ 1, 2, 3, 'a', 's', 34 ]

 

1.4 two-position operator ~~

Operator can be used instead of dual bit Math.floor () . Advantages bit double negation operator in that it performs the same operation faster.

Math.floor(4.9) === 4 //true

// abbreviated as:

~~4.9 === 4 //true

A number of | 0 can be rounded, also applies negative, NUM | 0

1.3 | 0 // 1
-1.9 | 0 // -1

Note, however, for integer is  ~~  result of the operation and  Math.floor ()  the same calculation results, and for the negative is not the same:

~~4.5 // 4
Math.floor(4.5) // 4
~~-4.5 // -4
Math.floor(-4.5) // -5

1.5 shorting operator

We know the logical && logical or || shorted operator, the operator is left to right in a short-circuit in operation to meet the requirements of the former, the latter is no longer performed; will be understood as:

&& to take false operation, from left to right judgment, if you encounter a false value, it returns a false value, will no longer be executed, otherwise the last true value

|| to get real operation, from left to right judgment, if you encounter a true value, it returns a true value, will no longer be executed, otherwise the last false value

let param1 = expr1 && expr2
let param2 = expr1 || expr2

Operator illustration

&& expr1 && expr2 if expr1 energy is converted to false returns expr1, otherwise expr2. Thus , the Boolean use environment , two operation result are true returns true, otherwise it returns false.

|| || expr2 expr1 if expr1 energy is converted to true returns expr1, otherwise expr2. Thus , in boolean environment ( in the if condition determination ) when used , both the operation result as long as there is a true, returns true ; both operation result are false returns false.

!! Expr if a single expression can be converted to true , then return false, returned otherwise true.

Therefore it can be used to do a lot of interesting things, such as the initial values ​​to variables:

let variable1

let variable2 = variable1 || 'foo'

If variable1 is the true value is returned directly behind the short circuit it will not be returned, if it is false, it returns back foo .

Can also be used for simple judgment, substituted lengthy if statement:

1 let variable = param && param.prop

If param If true value is returned param.prop property, otherwise param this false value, so in some places to prevent param is undefined when it comes to take its property caused by an error.

 

1.6  Analyzing parity & 1

 

A number of & 1 based on parity, also applies negative, NUM & 1

cons num = 3 ;
!! (and whether 1)                  // true 
!! (whether% 2)                  // true

 

Guess you like

Origin www.cnblogs.com/trccc9/p/11460066.html