js callback , ajax call back can observer mvvm

Refer : http://www.jb51.net/article/59447.htm


  do something other  || like android network after the net work down  do some thing    by param but android only can pass the param by interface callBack  just like click event  android -- web
    {

        class Demo{
            network(fun){

                        setTimeout(()=>{
                            fun.call(this);
                        },3000)
                  }
        }

    new Demo().network(()=>{
            alert(1);
      })
    }

帶參數的也比較簡單


  do something other  || like android network after the net work down  do some thing    by param but android only can pass the param by interface callBack  just like click event  android -- web
    {

        class Demo{
            network(fun){

                        setTimeout(()=>{
                            // use obj where in call the function and pass param 
                            fun.call(this,"d");
                        },3000)
                  }
        }

    // todo:// --> you can get the return value by this way
    new Demo().network((e)=>{
            alert(e);
      })
    }
   var _$ = function (id) {
    this.ele = document.getElementById(id);
   }

    // the prototype method need after new
   _$.prototype = {

       dod:function () {
         alert(1);
       },

       bind:function (eve,callback) {
           this.ele.addEventListener(eve,()=>{
               callback.call(this,this.getX());
           });

       },
       getX:function (e) {
           let eve = e||window.event;
           if(eve.pageX) {
               return 1;
           }else{
               return 0;
           }
       }

   }

   //why not rename
   window.$=function(id)
   {
       return  new _$(id);
   }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章