1. JS错误信息
1.1 SyntaxError 语法错误
// 1.变量名不规范
var 1 = 1;
var 1a = 1;
// 2.关键字不可以赋值
var new = 1; // Uncaught SyntaxError: Unexpected token 'new'
// 3.基本语法错误
var a = 6: // Uncaught SyntaxError: Unexpected token ':'
1.2 ReferenceError 引用错误
// 1. 变量或函数未声明
test(); //Uncaught ReferenceError: test is not defined
console.log(a); // Uncaught ReferenceError: a is not defined
// 2.给无法赋值的对象赋值的时候
var a = 1 = 2;// Uncaught SyntaxError: Invalid left-hand side in assignment
1.3 RangeError 范围错误
// 1. 数组长度超出可行范围
var arr = [1, 2, 3];
arr.length = 1; // Uncaught RangeError: Invalid array length
// 2.对象方法超出可行范围
var num = new Number(66.66);
console.log(num.toFixed(150));
// Uncaught RangeError: toFixed() digits argument must be between 0 and 100
1.4 TypeError 类型错误
// 1.调用不存在的方法
var obj = {};
obj.say();// Uncaught TypeError: obj.say is not a function
// 2.实例化原始值
var a = 1; // Uncaught TypeError: 1 is not a constructor
var a = new false; // Uncaught TypeError: false is not a constructor
1.5 URIError URI错误
// ? URI : Uniform Resource Identifier
// ? 统一资源标识符
// ? URL : Uniform Resource Locator
// ? 统一资源定位符
// ? URN : Uniform Resource Name
// ? 统一资源名称
var str = decodeURI('%dasaf%'); // Uncaught URIError: URI malformed
1.6 EvalError eval函数执行错误
JSON 与对象
var jsonData = '['+
'{'+
'"name": "abc"'+
'},'+
'{'+
'"name": "bcd"'+
'},'+
'{'+
'"name": "efg"'+
'}'+
']';
console.log(jsonData);
// ? json字符串 => json对象
var data = eval('('+ jsonData + ')');
console.log(data);
for(var i in data){
var item = data[i];
console.log(item.name);
}
1.7 Error
var error = new Error('代码错误了');
var sError = new SyntaxError('代码错误了');
var rError = new ReferenceError('代码错误了');
var tError = new TypeError('代码错误了');
console.log(error);
console.log(sError);
console.log(rError);
console.log(tError);
2. try-catch
try{
console.log('正常执行1');
console.log(a);
}catch(e){
console.log(e.name + ':' + e.message);
}finally{
console.log('正常执行3');
}
var jsonStr = '';
try{
if(jsonStr == ''){
throw 'JSON字符串为空';
}
console.log('我要执行了!!!!!');
var json = JSON.parse(jsonStr);
console.log(json);
}catch(e){
console.log(e);
var errorTip = {
name: '数据传输失败',
errorCode: '10010'
}
console.log(errorTip);
}
3. 严格模式
ECMAScript JavaScript语法规范, 方法规范<br /> 97 1.0<br /> 98 2.0<br /> 99 3.0 JS通行标准<br /> 07 4.0草案 <br /> 08 4.0中止 容易改善3.1 激进部分Harmony<br /> 更名ECMAScript5<br /> 09 5.0发布, Harmony => JS.NEXT, JS.next,next<br /> 11 5.1 ISO国际标准<br /> 13 ES6 = js.next js.next.next 7<br /> 13 ES6草案发布<br /> 15 ES6正式发布, ECMAScript2015
3.1 with不可以使用
// ES5 分为 正常模式 ,严格模式
// IE9及以下IE 不支持
// 3.0 => 严格模式
// 1.with不可以使用
'use strict';
var namespace = {
header: {
Jenny: {
a: 1,
b: 2
},
Ben: {
a: 3,
b: 4
}
},
sideBar: {
Crystal: {
a: 5,
b: 6
}
}
}
with(namespace.header.Ben){
console.log(a);
}
3.2 caller callee 不可以使用
'use strict';
function test(){
// console.log(arguments.callee);
test2();
}
function test2(){
console.log(test2.caller);
}
test();
'use strict';
function test(){
console.log(arguments.callee);
}
test();
3.3 不用var声明报错
'use strict';
a = 1;
3.4 全局的this指向window, 函数内部的指向undefined
'use strict';
function test(){
console.log(this);
}
console.log(this); // window{}
test(); // undefined
3.5 非严格模式.call(1) 会包装类
'use strict'
function test(a){
console.log(this);
}
test.call(1, 2);
3.6 函数的参数不能重复
'use strict'
function test(a, a){
console.log(a);
}
test(1, 2);