zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Python元组、列表、字典
    测试通过Word直接发布博文
    Python环境搭建(windows)
    hdu 4003 Find Metal Mineral 树形DP
    poj 1986 Distance Queries LCA
    poj 1470 Closest Common Ancestors LCA
    poj 1330 Nearest Common Ancestors LCA
    hdu 3046 Pleasant sheep and big big wolf 最小割
    poj 3281 Dining 最大流
    zoj 2760 How Many Shortest Path 最大流
  • 原文地址:https://www.cnblogs.com/diligenceday/p/3436680.html
Copyright © 2011-2022 走看看