闭包;
当碰到对象之间复杂的循环引用时,垃圾回收的判断逻辑非常复杂。故有时才用原型模型;
// 定义构造函数
function Person(name){
this.name=name;
}
// 方法定义到构造函数的原型上
Person.prototype.SayHello=function(){
console.log("hello,I'm "+this.name)
};
// 子类构造函数
function Employee(name,salary){
Person.call(this,name);//调用上层构造函数
this.salary=salary;
}
Employee.prototype=new Person();
Employee.prototype.ShowMeTheMoney=function(){
console.log(this.name+" $ "+this.salary)
}
var BillGates=new Person("Bill Gates");
BillGates.SayHello();
var SteveJobs=new Employee("Steve Jobs",1234);
SteveJobs.SayHello();
SteveJobs.ShowMeTheMoney();