자체 구조 함수

1837 단어 구조 함수
/**

 * @description  function 

 * @function

 * @obj this 

 */





if(!Function.prototype.bind){

	Function.prototype.bind = function(obj){

		var slice = [].slice,

			args = slice.call(arguments,1),

			self = this,

			nop = function(){},

			bound = function(){

				return self.apply(this instanceof nop ? this : (obj || {}),args.concat(slice.call(arguments)))

			}



		nop.prototype = self.prototype;



		bound.prototype = new nop();



		return bound

	}

}



/**

 * @description Class  

 * @function

 * @public

 */



var Class = function(parent){

	var Klass = function(){

		this.init.apply(this,arguments)

	}

	

	/* 

		@init method

	*/

	Klass.prototype.init = function(){}



	Klass.fn = Klass.prototype



	/*

	 * @ description  

	 * @ function

	 * @ public

	 * @ params {Object}  

	 * 

	 */

	

	if(parent){

		var subClass = function(){}

		subClass.prototype = parent;

		Klass.fn = new subClass();

	}



	/*

	 * @ description  

	 * @ function

	 * @ public

	 */

	Klass.proxy = function(func){

		var self = this;

		return (function(){

			func.apply(self,arguments)

		})

	}



	/*

	 * @ description 

	 */

	Klass.extend = function(obj){

		var extend = obj.extend

		for(var i in obj){

			if(obj.hasOwnProperty(i)){

				Klass[i] = obj[i]

			}

		}

		extend && extend(Klass)

	}



	Klass.include = function(obj){

		var included = obj.included

		for(var i in obj){

			if(obj.hasOwnProperty(i)){

				Klass.fn[i] = obj[i]

			}

		}

		included && included(Klass)

	}





	Klass.fn.proxy = Klass.proxy;



	return Klass



}



/*

	  Button

 */

var Button = new Class();

Button.include({

	init : function(element){

		this.element = $('#aa')

		this.element.click(this.proxy(this.click))

	}, 

	click : function(){}

})


좋은 웹페이지 즐겨찾기