2016-07-10 15 views
0

私は単純なラッパーだと思ったものをjqueryでラップしましたが、関数として引数を取る関数はありません(関数は呼び出されません)。単純なjqueryラッパーは完全には機能しません

class $Util { 
    // jquery wrapper 
    // use with .apply(this...) 
    static $() { 
     if (!this.$wrapper) 
      throw new ReferenceError('$Util.$: $wrapper not defined in context of this'); 
     var shift = [].shift; 
     var fn = shift.apply(arguments); 
     this.$wrapper[fn](arguments); 
     return this; 
    } 
} 

class Foo { 
    constructor() { 
     this.$wrapper = $('#foo'); 
     return this; 
    } 
    $() { 
     $Util.$.apply(this, arguments); 
     return this; 
    } 
} 

var f = new Foo().$('appendTo', $('body')); // no problem 
f.$('slideUp', function() { 
    console.log(" i never log !"); 
}); 
f.$wrapper.slideUp(function() { 
    console.log(" but i do !"); 
}); 

https://jsfiddle.net/0v0b6k5q/

答えて

1

に...引数の引数を変更するいくつかの理由で、トリックをしました。

class $Util { 
     // jquery wrapper 
     // use with .apply(this...) 
     static $() { 
      if (!this.$wrapper) 
       throw new ReferenceError('$Util.$: $wrapper not defined in context of this'); 
      var shift = [].shift; 
      var fn = shift.apply(arguments); 
      this.$wrapper[fn](...arguments); 
      return this; 
     } 
    } 
関連する問題