一、原型及原型链
//给Person构造函数添加原型对象
Person.prototype = {
constructor: Person, // 构造函数的原型对象中的构造函数指向他本身
name: "",
eat: function () {
}
}
//Person 构造函数
function Person() {
this.color = "";
this.sleep = function () {
}
}
var p = new Person(); // 对构造函数进行实例化,生成一个对象
console.log(p);
console.log(p.__proto__); //原型对象Person
console.log(p.__proto__.__proto__); //原型链 object 的原型对象
二、严格模式
<script>
"use strict" // 在代码前写上这句话,就代表的是严格模式,后面的代码就要写的规范,否则会报错
var x = 10;
m = 20;
</script>
三、异常处理
try: 测试代码块的异常
catch: 处理异常
throw:抛出异常
finally: 无论是否有触发异常,最后该语句都会执行
- 如果try语句中有异常,则执行catch语句,如果 try语句中没有异常,就不执行catch语句,直接执行finally语句。
- 如果catch中有错误的语句,finally中没有,则报catch中的错误;如果catch中有错误的语句,finally中也有,则报finally中的错误,catch中的不报。先执行完所有的正常语句,最后再报错误信息
- e 是错误信息
// 1. try 语句中没有错误
try{
console.log(0);
}catch (e){
console.log(1);
console.log(hello);
}finally {
console.log(2);
} // 结果是:0 2
// 2. try 语句有错误,catch有错误
try{
a();
}catch (e){
console.log(1);
console.log(hello);
}finally {
console.log(2);
} // 结果是:1 2 Uncaught ReferenceError: hello is not defined
// 3. try 语句中没有错误,catch有错误,finally也有错误
try{
console.log(a, b, c);
}catch (e){
console.log(1);
console.log(hello);
}finally {
console.log(2);
console.log(word);
} // 结果是:1 2 Uncaught ReferenceError: word is not defined
// 4. try 语句的嵌套
try{
a.b.c();
}catch (e){
console.log(1);
// console.log(hello);
try{
console.log(word);
}catch (e){
console.log(hello);
}
}finally {
console.log(2);
} // 结果是:1 2 Uncaught ReferenceError: hello is not defined