javascript 讓執行個體的方法都非同步執行

來源:互聯網
上載者:User
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)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.