mirror of
				https://github.com/flynx/Course-JavaScript.git
				synced 2025-10-31 03:50:10 +00:00 
			
		
		
		
	type coercion...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									87f27348a7
								
							
						
					
					
						commit
						79fb72406d
					
				| @ -149,7 +149,32 @@ | |||||||
| 
 | 
 | ||||||
| // Automatic type coercion
 | // Automatic type coercion
 | ||||||
| //
 | //
 | ||||||
| // XXX
 | // In JavaScript most operations are defined on operands of the same 
 | ||||||
|  | // type. If the types are different JavaScript will try and convert 
 | ||||||
|  | // one of the values to make the types match.
 | ||||||
|  | //
 | ||||||
|  | // In most cases this is fully transparent, for example:
 | ||||||
|  | 	'42' == 42 // -> true
 | ||||||
|  | 	2 * '21' // -> 42
 | ||||||
|  | 	undefined == null // -> true
 | ||||||
|  | 
 | ||||||
|  | // But in cases where the same operation is defined for both types
 | ||||||
|  | // the result may seem less predictable, for example `+` defines both
 | ||||||
|  | // number addition and string concatination:
 | ||||||
|  | 	1 + 2 // -> 3
 | ||||||
|  | 	'a' + 'b' // -> 'ab'
 | ||||||
|  | 
 | ||||||
|  | // But when mixed it reverts to constructor:
 | ||||||
|  | 	1 + '2' // -> '12'
 | ||||||
|  | 
 | ||||||
|  | // This feature can both help make the code simpler and more generic if
 | ||||||
|  | // used conciously and at the same time can prove quite frustrating if
 | ||||||
|  | // neglected.
 | ||||||
|  | //
 | ||||||
|  | // This neglect and carelessness is the main reason it is quite popular
 | ||||||
|  | // to avoid type coercion and instead overuse strict comparisons and 
 | ||||||
|  | // deffensively over-check everything, at times raised to such levels as
 | ||||||
|  | // to define whole languages (like TypeScript) around this.
 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| // Type checking
 | // Type checking
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user