【颠覆认知的JS】- 3. 循环、引用值初识、显示及隐式类型转换

48 阅读1分钟

循环

  • for(let i = init; i < end; i++){}
  • do {} while (condition)
  • while(condition) {}

引用值

  • array object function date RegExp
  • array
  • object

类型转换

  • typeof

  • number string boolean object undefined function

  • typeof(null) -> 'Object': null 刚开始是为了指定空对象,实际上是一个指空对象的指针,也是空对象的占位符

  • typeof(undefined) -> 'undefined'

  • typeof(NaN) -> 'number'

  • 如何判断是否为NaN: IsNaN()

  • 先调用Numer()

  • 再check

  • 显示类型转换

  • 即强制手动转换

  • Number() parseInt() parseFloat() String() Boolean()

  • Number()

  • Number('1abc23') -> NaN (ascii转换需要用String.prototype.charCodeAt())

  • Number(null) -> 0

  • Number(undefined) -> NaN

  • parseInt()

  • parseInt(true) || parseInt(null) || parseInt(undefined) || parseInt(NaN) -> NaN

  • parseInt(3.5) -> 3

  • parseInt('1abc23') -> 1

  • 隐式类型转换

  • js偷偷转换

  • ==