@nemos
2017-05-06T03:46:52.000000Z
字数 1057
阅读 702
web
变量提升: 所有变量声明都会提到所在作用域的最前处
标签语法
top:
for (var i = 0; i < 3; i++){
for (var j = 0; j < 3; j++){
if (i === 1 && j === 1) break top;
console.log('i=' + i + ', j=' + j);
}
}
typeof关键字
typeof x // 判断内置对象类型
// 仅支持 num,str,bool,func,undef,其他均为obj
typeof v === "undefined" // 判断变量是否被定义
返回undefined的情况
// 变量声明了,但没有赋值
var i;
i // undefined
// 调用函数时,应该提供的参数没有提供,该参数等于undefined
function f(x) {
return x;
}
f() // undefined
// 对象没有赋值的属性
var o = new Object();
o.p // undefined
// 函数没有返回值时,默认返回undefined
function f() {}
f() // undefined
假值
* undefined
* null
* false
* 0
* NaN
* ""
或''
(空字符串)
number类型
js内部所有数字都以64位浮点存储
parseInt(obj, base) // 转化数字
parseFloat() // 同上
Number() // 转化数字,不能转化返回NaN
String() // 转化字符
btoa(string) // base64编码
atob('SGVsbG8gV29ybGQh') // "Hello World!"
js 对象和字典差不多是一个东西
数组为以数组为键名的特殊对象
f.name // 返回参数括号前的函数名
f.length // 返回参数个数
f.toString() // 返回函数源码,内部注释也会返回
arguments // 函数参数的类数组对象,不定参时使用
arguments.callee // 返回对应函数
错误处理
function throwit() {
throw new Error('');
}
function catchit() {
try {
throwit();
} catch(e) {
console.log(e.stack); // print stack trace
} finally {
console.log()
}
}
catchit()
// Error
// at throwit (~/examples/throwcatch.js:9:11)
// at catchit (~/examples/throwcatch.js:3:9)
// at repl:1:5