1.错误信息
//1.SyntaxError 语法错误var 1 = 1;var 1ab = 1;// 关键字赋值new = 5;function = 1;// 基本的语法错误var a = 5;// 2.ReferenceError//变量或者函数未被声明test();//给无法被赋值的对象赋值的时候var a = 1 =2;var a = 1;console.log(a) = 1;// JS错误信息类型// 3.RangeError 范围错误// 数组长度赋值为负数var arr = [1,2,3];arr.length=-1;console.log(arr);// 对象方法参数超出可行范围var num = new Number(66.66);console.log(num.toFixed(-1));// 4.TypeError 类型错误// 调用不存在的方法123();var obj = {};// obj.say 不会报错因为浏览器会认为是一个属性没有赋值但是obj.say()报错是一个属性不是fn(){}obj.say();// 实例化原始值var a = new 'string';var a = new 123;// 5. URIError URI错误//URI: UNIFError URI错误//URI:UNIFORM RESOURCE IDENTIFIER// 统一资源标识符//URN: UNIFORM RESOURCE LOCATOR// 统一资源定位符//URL: UNIFORM RESOURCE NAME// 统一资源名称//URL:http://www.baidu.com/news#today// ftp://www.baidu.com/ftp#developer//URN: www.baidu.com/ftp#developer -> ID// href="tel:13900000000"// href='mailto:523579987@qq.com'var myUrl = 'http://www.baidu.cin?name=艾小野';var newUrl = encodeURI(myUrl);console.log(newUrl);var newNewUrl = decodeURI(newUrl);console.log(newNewUrl);//6.EvalError创建一个error实例,表示错误的原因:与 eval() 有关。
2.try_catch
try语句包含了由一个或者多个语句组成的try块, 和至少一个catch块或者一个finally块的其中一个,或者两个兼有, 下面是三种形式的try声明:
try...catchtry...finallytry...catch...finally
catch子句包含try块中抛出异常时要执行的语句。也就是,你想让try语句中的内容成功, 如果没成功,你想控制接下来发生的事情,这时你可以在catch语句中实现。 如果在try块中有任何一个语句(或者从try块中调用的函数)抛出异常,控制立即转向catch子句。如果在try块中没有异常抛出,会跳过catch子句。finally子句在try块和catch块之后执行但是在下一个try声明之前执行。无论是否有异常抛出或捕获它总是执行。
try{
console.log('正常执行1');
console.log('a');
console.log('b');
console.log('正常执行2');
}catch(e){
console.log(e.name+':'+e.message);
}finally{
console.log('正常执行3');
}
console.log('正常执行4');
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.严格模式
ES5 正常模式 严格模式<br /> IE9及以下IE<br /> 3.0 -> 严格模式
//严格模式的三种写法
'use strict'; //全局写法
function test() {
'use strict'; //局部函数写法
}
var test = (function () {
'use strict'; //局部函数写法
})();
caller callee 严格模式下不可用
'use strict';
function test1(){
test2();
}
test1();
function test2(){
console.log(test2.caller);
}
with 改变作用域 严格模式下不可用
非严格模式下
var a = 1;
var obj ={
a:2
}
function test() {
var a = 3;
with(window){
console.log(a); //1
}
}
test();
严格模式下
报错
解决命名空间重复的问题
window.onload = function () {
init();
}
function init() {
initSlider;
initSideBar;
}
var initSlider = (function () {
var a = 1;
console.log(a);
})();
var initSideBar = (function () {
var a = 2;
console.log(a);
})();
以前的解决方法
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);
}
//严格模式下
'use strict';
a = 1; //报错
var a = b = 1; //报错
function test(){
console.log(this); //严格模式下this必须声明
}
test();//undefined
test.call(1); // 1
// 函数的参数不能重复
function test(a,a){
console.log(a);
}
test(1,2);
//不会报错
var obj = {
a:1,
a:2
}
console.log(obj.a);
eval('var a = 1;console.log(a)'); //非严格模式下 eval是全局windows
//严格模式下 eval有自己的作用域
console.log(a); //报错
//非严格模式下
a = 1; //报错
var a = b = 1; //报错
function test(){
console.log(this); //严格模式下this必须声明
}
test();//window
test.call(1); // Number{1}
