Javascript methods that can not be called from jquery objects?

后端 未结 2 1784
余生分开走
余生分开走 2021-01-18 02:51

I was reading Learning jQuery 1.3 (Jonathan Chaffer and Karl Swedberg) and while sorting table, they used .get() before calling .sort(), and said <

相关标签:
2条回答
  • 2021-01-18 03:32

    jQuery objects currently support 3 array methods:

    var methods = 'pop push reverse shift sort splice unshift concat join slice toString indexOf lastIndexOf filter forEach every map some reduce reduceRight'.split(' ')
    var implemented = $.grep(methods, function(m) {
        return $.prototype[m] == Array.prototype[m];
    });
    console.log(implemented); // => ["push", "sort", "splice"]
    

    They also have slice, but it's not the same slice as arrays have:

    $.prototype.slice === Array.prototype.slice // => false
    
    0 讨论(0)
  • 2021-01-18 03:37

    jQuery does have a .sort method, it just isn't officially documented because it does not follow the usual format of jQuery methods.

    The only methods that are supported are the ones listed in the api.

    .sort is implemented as:

    $.fn.sort = [].sort;
    

    You can add your own additional array methods as needed in the same way.

    $.fn.reverse = [].reverse;
    

    If .sort isn't implemented in your version of jQuery, implement it yourself.

    0 讨论(0)
提交回复
热议问题