Skip to content

Instantly share code, notes, and snippets.

@zD98
Created March 23, 2016 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zD98/44daee93aec8e2311a18 to your computer and use it in GitHub Desktop.
Save zD98/44daee93aec8e2311a18 to your computer and use it in GitHub Desktop.
设计模式-混入模式

##Mixin模式 ###总结 函数复用, 从其它对象原型中共享属性和方法的对象 子类化 ###例子

function augment(receivingClass, givingClass){
	//覆盖&扩展
	if(arguments[2]){
		for(var i=2,len=arguments.length; i<len;i++){
			receivingClass.prototype[arguments[i]] = 
				givingClass.prototype[arguments[i]];
		}
	}else{
		//扩展原型
		for(var methodName in givingClass.prototype){
			if(!Object.hasOwnProperty(receivingClass.prototype, methodName)){
				receivingClass,prototype[methodName] =
					 givingClass.prototype[methodName];				
			}
		}
	}

}

###分析 1. 子类化, 就是子类化.. 2. 有助于减少系统中的重复功能和增加函数复用。 3. 当需要在各个对象中实现各种共享函数时, 可以将函数抽象出来用来做Mixin 4. Dojo框架中UI组件类的使用中, 使用了Mixin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment