Skip to content

Commit 1c7d34f

Browse files
update
1 parent 604a8d3 commit 1c7d34f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,52 @@ d.fn1();
17091709
e.fn1();
17101710
```
17111711

1712+
- 为了区分对象的类型,我们用typeof操作符获取对象的类型,它总是返回一个字符串
1713+
1714+
```javascript
1715+
typeof 123; // 'number'
1716+
typeof NaN; // 'number'
1717+
typeof 'str'; // 'string'
1718+
typeof true; // 'boolean'
1719+
typeof undefined; // 'undefined'
1720+
typeof Math.abs; // 'function'
1721+
typeof null; // 'object'
1722+
typeof []; // 'object'
1723+
typeof {}; // 'object'
1724+
1725+
//可见,number、string、boolean、function和undefined有别于其他类型。特别注意null的类型是object,Array的类型也是object,如果我们用typeof将无法区分出null、Array和通常意义上的object——{}。
1726+
1727+
//总结一下,有这么几条规则需要遵守:
1728+
1729+
//不要使用new Number()、new Boolean()、new String()创建包装对象;
1730+
1731+
//用parseInt()或parseFloat()来转换任意类型到number;
1732+
1733+
//用String()来转换任意类型到string,或者直接调用某个对象的toString()方法;
1734+
1735+
//通常不必把任意类型转换为boolean再判断,因为可以直接写if (myVar) {...};
1736+
1737+
//typeof操作符可以判断出number、boolean、string、function和undefined;
1738+
1739+
//判断Array要使用Array.isArray(arr);
1740+
1741+
//判断null请使用myVar === null;
1742+
1743+
//判断某个全局变量是否存在用typeof window.myVar === 'undefined';
1744+
1745+
//函数内部判断某个变量是否存在用typeof myVar === 'undefined'。
1746+
1747+
//最后有细心的同学指出,任何对象都有toString()方法吗?null和undefined就没有!确实如此,这两个特殊值要除外,虽然null还伪装成了object类型。
1748+
1749+
123.toString(); // SyntaxError
1750+
1751+
//遇到这种情况,要特殊处理一下:
1752+
1753+
123..toString(); // '123', 注意是两个点!
1754+
(123).toString(); // '123'
1755+
1756+
```
1757+
17121758
- 个人经历面试题
17131759

17141760
```javascript

0 commit comments

Comments
 (0)