前端Javascript中圣杯模式

981 阅读1分钟
	圣杯模式
	function inherit(Target,Origin){
		function F(){};
		F.prototype = Origin.prototype;
		Target.prototype = new F();
		Target.prototype.constuctor = Target;
		Target.prototype.uber = Origin.prototype;
	}
	Father.prototype.LastName = "deng";
	function Father(){};
	function Son(){};
	inherit(Son,Father);
	var son = new Son();
	var father = new Father;

	高级圣杯
	var inherit = (function (){
		var F = function(){}
		return function(Target,Origin){
			F.prototype = Origin.prototype;
			Target.prototype = new F();
			Target.prototype.constuctor = Target;
			Target.prototype.uber = Origin.prototype;
		}
	})
	 Father.prototype.LastName = "deng";
	function Father(){};
	function Son(){};
	inherit(Son,Father);
	var son = new Son();
	var father = new Father;

	利用圣杯实现闭包中属性私有化的作用
	function Deng(name,wife){
		var preparewife = "xiaoxu";
		this.name = name;
		this.wife = wife;
	}
	this.divorce = function (){this.wife = preparewife;}
	this.changePreparewife = function(target){
		preparewife = target;
	}
	this.sayPreparewife = function(){
		console.log(preparewife);
	}
	var deng = new Deng('deng','xiaoxu');