zoukankan      html  css  js  c++  java
  • 自己构造构造函数

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

      

  • 相关阅读:
    Java
    oracle与mysql(2)
    oracle与mysql
    junit中的assert方法总结
    java Future用法和意义一句话击破
    Java序列化中的SerialVersionUid
    Nginx了解
    现如今的CDN网站加速技术,细说CDN
    slf4j日志的使用
    IDEA 快捷键整理
  • 原文地址:https://www.cnblogs.com/xiaohui108/p/4235043.html
Copyright © 2011-2022 走看看