[关闭]
@nemos 2017-05-06T03:46:52.000000Z 字数 1057 阅读 702

javascript

web


基础拾遗

重要概念

变量提升: 所有变量声明都会提到所在作用域的最前处

标签语法

  1. top:
  2. for (var i = 0; i < 3; i++){
  3. for (var j = 0; j < 3; j++){
  4. if (i === 1 && j === 1) break top;
  5. console.log('i=' + i + ', j=' + j);
  6. }
  7. }

typeof关键字

  1. typeof x // 判断内置对象类型
  2. // 仅支持 num,str,bool,func,undef,其他均为obj
  3. typeof v === "undefined" // 判断变量是否被定义

返回undefined的情况

  1. // 变量声明了,但没有赋值
  2. var i;
  3. i // undefined
  4. // 调用函数时,应该提供的参数没有提供,该参数等于undefined
  5. function f(x) {
  6. return x;
  7. }
  8. f() // undefined
  9. // 对象没有赋值的属性
  10. var o = new Object();
  11. o.p // undefined
  12. // 函数没有返回值时,默认返回undefined
  13. function f() {}
  14. f() // undefined

假值
* undefined
* null
* false
* 0
* NaN
* ""''(空字符串)
number类型
js内部所有数字都以64位浮点存储

方法

  1. parseInt(obj, base) // 转化数字
  2. parseFloat() // 同上
  3. Number() // 转化数字,不能转化返回NaN
  4. String() // 转化字符
  5. btoa(string) // base64编码
  6. atob('SGVsbG8gV29ybGQh') // "Hello World!"

js 对象和字典差不多是一个东西
数组为以数组为键名的特殊对象

  1. f.name // 返回参数括号前的函数名
  2. f.length // 返回参数个数
  3. f.toString() // 返回函数源码,内部注释也会返回
  4. arguments // 函数参数的类数组对象,不定参时使用
  5. arguments.callee // 返回对应函数

错误处理

  1. function throwit() {
  2. throw new Error('');
  3. }
  4. function catchit() {
  5. try {
  6. throwit();
  7. } catch(e) {
  8. console.log(e.stack); // print stack trace
  9. } finally {
  10. console.log()
  11. }
  12. }
  13. catchit()
  14. // Error
  15. // at throwit (~/examples/throwcatch.js:9:11)
  16. // at catchit (~/examples/throwcatch.js:3:9)
  17. // at repl:1:5
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注