当前位置:主页 > 查看内容

JavaScript的两大类内建数据类型

发布时间:2021-08-07 00:00| 位朋友查看

简介:JavaScript的数据类型在大的方向上分为两类:1)primitive types and 2)object tyeps。 其一 primitive types包括常规的 numbers,string, booleans 以及特殊类型的 null 和 undefined。而且以上五类都是immutuable types; 其二,object types 包括object,……

JavaScript的数据类型在大的方向上分为两类:1)primitive types and 2)object tyeps。

其一 primitive types包括常规的 numbers,string, booleans 以及特殊类型的 null 和 undefined。而且以上五类都是immutuable types;

其二,object types 包括object,以及特殊类型的object即array。其他比如 Set,Map,typed array, RegExp and Date types.

一、Numbers

Numeric literal 表示 十六进制,二进制和八进制:

  1. //integer literals 
  2. > 0xff 
  3. 255 
  4. > 0b1011 
  5. 11 
  6. > 0o377 
  7. 255 
  8. > 377 
  9. 377 
  10. //floating-point literals 
  11. undefined 
  12. > 6.02e23 
  13. 6.02e+23 
  14. > 1.47e-23 
  15. 1.47e-23 
  16. //Arithmetic  
  17. Math.hypo 
  18.  
  19. //Infinity 
  20. Infinity                    // A positive number too big to represent 
  21. Number.POSITIVE_INFINITY    // Same value 
  22. 1/0                         // => Infinity 
  23. Number.MAX_VALUE * 2        // => Infinity; overflow 
  24.  
  25. -Infinity                   // A negative number too big to represent 
  26. Number.NEGATIVE_INFINITY    // The same value 
  27. -1/0                        // => -Infinity 
  28. -Number.MAX_VALUE * 2       // => -Infinity 
  29.  
  30. NaN                         // The not-a-number value 
  31. Number.NaN                  // The same value, written another way 
  32. 0/0                         // => NaN 
  33. Infinity/Infinity           // => NaN 
  34.  
  35. Number.MIN_VALUE/2          // => 0: underflow 
  36. -Number.MIN_VALUE/2         // => -0: negative zero 
  37. -1/Infinity                 // -> -0: also negative 0 
  38. -0 
  39.  
  40. // The following Number properties are defined in ES6 
  41. Number.parseInt()       // Same as the global parseInt() function 
  42. Number.parseFloat()     // Same as the global parseFloat() function 
  43. Number.isNaN(x)         // Is x the NaN value? 
  44. Number.isFinite(x)      // Is x a number and finite? 
  45. Number.isInteger(x)     // Is x an integer
  46. Number.isSafeInteger(x) // Is x an integer -(2**53) < x < 2**53? 
  47. Number.MIN_SAFE_INTEGER // => -(2**53 - 1) 
  48. Number.MAX_SAFE_INTEGER // => 2**53 - 1 
  49. Number.EPSILON          // => 2**-52: smallest difference between numbers 
  50. // 浮点数  
  51. 18,437,736,874,454,810,627 只有这么多浮点数,能被表示出来。 
  52. // rouding problems  
  53. //BigInt 
  54.  
  55. //Date and time 
  56. let timestamp = Date.now();  // The current time as a timestamp (a number). 
  57. let now = new Date();        // The current time as a Date object. 
  58. let ms = now.getTime();      // Convert to a millisecond timestamp
  59. let iso = now.toISOString(); // Convert to a string in standard format. 

二、String and Text

  1. // 1.string literals 
  2. // 2.escape sequences  
  3. // 3.string methods 
  4. // 4.template literals (tagged template literals) 
  5. // 5.Pattern Matching  
  6. /[1-9][0-9]*/;  

三、Boolean Values

只有 true 和 false 这两项。

四、null and undefined

  1. > typeof(null
  2. 'object' 

五、Symbols

  1. let s = Symbol.for("shared"); 
  2. let t = Symbol.for("shared"); 
  3. s === t          // => true 
  4. s.toString()     // => "Symbol(shared)" 
  5. Symbol.keyFor(t) // => "shared" 

六、Global

  • Global constants like undefined, Infinity, and NaN
  • Global functions like isNaN(), parseInt() (§3.9.2), and eval() (§4.12)
  • Constructor functions like Date(), RegExp(), String(), Object(), and Array() (§3.9.2)
  • Global objects like Math and JSON (§6.8)

七、Immutable Primitives and Mutable Object Referece

  1. function equalArray(a, b) { 
  2. ... if (a === b) return true
  3. ... if (a.length !== b.length) return false
  4. ... for (let i = 0; i < a.length; i++) { 
  5. ..... if (a[i] !== b[i]) return false
  6. ..... } 
  7. ... return true
  8. ... } 

八、Type Conversions

implicite conversion and explicite conversions

九、Variable Declaration and Assignment

  1. // Destructuring Assignment 
  2. [x,y] = [x+1,y+1];  // Same as x = x + 1, y = y + 1 
  3. [x,y] = [y,x];      // Swap the value of the two variables 
  4. // Same as const sin=Math.sin, cos=Math.cos, tan=Math.tan 
  5. const {sin, cos, tan} = Math; 
  6. //此处与python的用法完全一致。 

本文转载自网络,原文链接:https://www.toutiao.com/i6910854970648658446/
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐