201506081340_《JavaScript秘密花园》

1. typeof的唯一作用:检测变量是否已定义。例如:

typeof foo !== 'undefined'

此外,推荐使用:Object.prototype.toString();进行检测变量类型。


2.instanceof 操作符应该仅仅用来比较来自同一个 JavaScript 上下文的自定义对象。
new String('foo') instanceof String; // true
new String('foo') instanceof Object; // true

'foo' instanceof String; // false
'foo' instanceof Object; // false


3.技巧

a. 转换为字符串
'' + 10 === '10'; //true

b.转换为数字
  +'10' === 10; //true
Number('010') === 10;
parseInt('010',10) ===10 //true

c.转化成布尔值
!!'foo' //true
!!'0' //true
!!'1' //true
    !!'-1' //true
!!{} //true
!!'' //false
4.
 
 


前端-语言
原文地址:https://www.cnblogs.com/beesky520/p/4561014.html