继承/闭包
传统形式 -> 原型链
借用构造函数
共享原型
Father.prototype.lastName = "deng";
function Father(){
}
function Son(){
}
Son.prototype = Father.prototype;
var son =new Son();
var father =new Father();
//共享原型,但Son.prototype改变 Father,protottype也跟着改变
Father.prototype.lastName = "deng";
function Father(){
}
function Son(){
}
Son.prototype = Father.prototype;
Son.prototype.sex = "male";
var son =new Son();
var father =new Father();
共享原型,但Son.prototype改变 Father,protottype也跟着改变
圣杯模式
function inherit(Target,Origin){
function F(){};
F.prototype = Origin.prototype;
Target.prototype = new F();
}
Father.prototype.lastName = "deng";
function Father(){
}
function Son(){
}
inherit(Son.Father);
var son =new Son();
var father =new Father();
// YUI3 库 中的方法 圣杯模式
var inherit = (function(){
var F = function(){}; //私有化变量 外部访问不到
return function(Target,Origin){
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constructor = Target;
Target.prototype.uber = Origin.prototype;
}
}());
// 闭包中的函数封装
function Deng(name,wife){
let prepareWife = "xiaozhang";
this.name = name;
this.wife = wife;
this.divorce = function(){
this.wife = preapareWife;
}
this.changePreapareWife = function(){
prepareWife = target;
}
this.sayPrepareWife = function(){
console.log(prepareWife);
}
}
let deng = new Deng('deng','xiaoliu');
闭包中的函数封装