• JS:采摘自JS精粹


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <body>
    <script>
    //无论是变量名字还是,对象的属性都不能是 保留字;
    //继承
    Object.prototype.method = function(method,fn){
        return this[method] = fn;
    };
    //继承方法1 复制继承
    Function.prototype.method('new',function(){
        var that = Object.create(this.prototype);
        var other = this.apply(that, arguments);
        return (typeof other === 'object' && other) || that;
    });
    //继承方法2: 原型继承
    Object.prototype.method('inherits',function(parent){
        this.prototype = new parent();
        //return this;
    });
    
    function aa(){};
    aa.prototype.fn = function(){alert(1)};
    aa.prototype.fn1 = function(){alert(2)};
    
    //var bb = function(){}
    //bb.prototype = aa.new();
    
    //var bb = function(){};
    //bb.inherits( aa )
    
    //aa.fn1() //错误
    var bb = (new aa)
    bb.fn1() // alert(1);
    //函数的属性和prototype是不一样的,但是用new了函数以后,返回的obj拥有了protype(继承)的方法和属性
    
    //Model化开发
    var mammal = function(spec){
        var that = {};
        
        that.get_name = function(){
            return spec.name;
        };
        that.says = function(){
            return spec.saying || '';
        };
        
        return that;
    };
    var myMammal = mammal( {name : 'herb'} );
    
    //差异化,复用mammal
    var cat = function(spec){
        spec.saying = spec.saying || 'meow';
        var that = mammal(spec);
        that.purr = function(n){
            var i, s = '';
            for(var i=0; i<n; i++){
                if(s){
                    s += '_';
                };
                s += 'r';
            };
            return s;
        };
        that.get_name = function(){
            return that.says() + ' ' +spec.name+ ' ' + that.says();
        };
        return that;
    };
    var myCat = cat({name : 'xx'});
    
    
    //部件__  ->_->话说自定义事件和常见啊 !>_<!
    var eventuality = function(that){
        var registry = {};
        that.fire = function(event){
            var array,
                func,
                handler,
                i,
                type = typeof event === 'string' ? event : event.type;
            
            if( registry.hasOwnProperty(type) ){
                array = registry[type];
                for(var i=0; i<array.length; i++){
                    handler = array[i];
                    func = handler.func;
                    func();
                }
            };
        };
        that.on = function(type, method, parameters){
            //registry[type] || ( registry[type] = {} );
            var handler = {
                method : method,
                parameters : parameters
            };
            if( registry.hasOwnProperty(type) ){
                registry[type].push( handler );
            }else{
                registry[type] = [handler];
            };
            return this;
        };
        return that;
    }
    </script>
    </body>
    </html>
  • 相关阅读:
    云时代架构阅读笔记时十
    云时代架构”经典文章阅读感想九
    “云时代架构”经典文章阅读感想八
    antlr4
    Centos7定时脚本
    JDBC连接hive失败,一种原因的解决方法
    Linux实用操作命令
    secureCRT下载虚拟机文件到本地
    OpenFeign执行POST请求类型以及Python的requests.post()方法
    DataSphere安装配置
  • 原文地址:https://www.cnblogs.com/diligenceday/p/3436680.html
Copyright © 2020-2023  润新知