zoukankan      html  css  js  c++  java
  • javascript 让实例的方法都异步执行

    var C = function(){
        this._methodList = [];
    }
    C.prototype.fire = function(obj){
        var instance = this;
        while(obj = this._methodList.shift()){
            if(isFinite(obj.fn)){
                var time = new Date;
                instance.timeoutID = setTimeout(function(){
                    instance.fire();
                    instance._delayTime = new Date - time;
                },obj.fn);
                break;
            }else{
                this._result = obj.fn.apply(instance,obj.args)
            }
        }
    }
    C.prototype.wait = function(ms){
        this._methodList.push({
            fn:ms
        });
        return this;
    }
    C.prototype.abort = function(){
        createTimeout(this.timeoutID);
        this.fire();
    }
    C.extend = function(name,body){
        if(typeof name === "object"){
            for(var i in name){
                if(name[i] !== void 0){
                    C.extend(i, name[i])
                }
            }
        }else{
            if(typeof body === "function"){
                var method = C.prototype[name] = function(){
                    var obj = {
                        args:arguments,
                        fn:body
                    }
                    this._methodList.push(obj);
                    this.fire();
                    return this._result !== void 0 ? this : this;
                }
                method.toString = function(){
                    return body+"";
                }
            }else{
                this.prototype[name] = body;
            }
        }
        return this;
    }
    C.extend({
        aaa:33,
        attr:function(prop){
            return this[prop];
        },
        getName:function(){
            return this.name;
        },
        setName:function(name){
            this.name = name;
        }
    
    });
    
    var c = new C;
    console.log(c);
    c.attr(aaa);
    c.setName("司徒正美").wait(1000).getName();
    console.log(c.name);
    console.log(c.setName)
    
    
  • 相关阅读:
    微信开发笔记-调用自定义分享接口
    应试教育
    AJAX学习笔记
    日志管理-Log4net
    linq学习笔记
    委托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式
    Webservice服务创建、调用笔记
    设计模式(23)---迭代器模式
    设计模式(22)---备忘录模式
    设计模式(21)---访问者模式
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/2145952.html
Copyright © 2011-2022 走看看