JS 계승의 실현

1723 단어 js
<!DOCTYPE HTML>
<html>
	<head>
		<script type="text/javascript">
		//  
		function clone(object){
			function F(){};
			F.prototype = object;
			return new F;
		};
		
		function Mixin(){}
		Mixin.prototype = {
			serialize : function(){
				var output = [];
				for(var key in this){
					if(key != 'serialize'){
						output.push(key + ':' + this[key]);
					}
				}
				return output.join(',');
			}
		};
		
		/**
		 *  receivingClass givingClass 
		 *  , 
		 * receivingClass  
		 * givingClass  
		 */
		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(!receivingClass.prototype[methodName]){
						receivingClass.prototype[methodName] = givingClass.prototype[methodName];
					}
				}
			}
		}
		
		//  
		function Author(){}
		Author.prototype = {
			'nationality' : 'China',
			'name' : 'Jack',
			'gender' : 'male',
			'mail' : '[email protected]'
		}
		
		//  Author Mixin serialize 
		augment(Author, Mixin, 'serialize');
		
		console.info(new Author().serialize());
		
		</script>
	</head>
</html>

좋은 웹페이지 즐겨찾기