码迷,mamicode.com
首页 > 其他好文 > 详细

类的创建

时间:2014-09-02 00:02:23      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   java   ar   for   div   cti   log   

/**
 * @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(){}
})

  

类的创建

标签:des   blog   io   java   ar   for   div   cti   log   

原文地址:http://www.cnblogs.com/xiaohui108/p/3950448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!