简体   繁体   中英

Can anyone help me understand this? JavaScript 'splice' method in prototype object

can anyone help me understand this? When i have in my prototype methods "splice" the result display in console log is "select [div#index]" without 'splice' this looks like "select {0: div#index, length: 1}".

 select.prototype = {
         splice: function () {
         [].splice.apply(this, arguments);
     },
 }

PHOTOS:

结果 1:

结果 2 没有“拼接”:

Script:

(function () {
    var doc = document;

    // it is for the test
    function select(sel) {

        if (sel) { this.push(query(sel)); }
        return this;
    }

    function query(selector) {
        let elems = [];
        var el = doc.querySelectorAll(selector);

            for (let i = el.length; i--; elems.unshift(el[i])) { }; 
        return elems;
    }

    select.prototype = {
        length: 0,

        push: function (elems) {
            [].push.apply(this, elems);
            return this;
        },
        // THIS METHOD
         splice: function () {
             [].splice.apply(this, arguments);
        },   

    };

    window['Q'] = function(selector) {
        return new select(selector);
    }

  console.log(window);
})();

Q("#index") to display result

It's using the Array.prototype.splice function on the select object rather than on an array. That's perfectly fine, splice (and several other array functions) is intentionally generic. As long as the object it sees as this has length and properties named with numeric strings, like arrays do, it'll work.

It's probably worth mentioning that the way it's using splice is over-complicated, (ever so slightly) inefficient (it creates and throws away a completely pointeless array), and doesn't return the deleted elements like splice on an array does (because it doesn't return the return value of Array.prototype.splice ). This:

splice: function () {
     [].splice.apply(this, arguments);
},

should simply be:

splice: Array.prototype.splice

Example:

 (function () { var doc = document; // it is for the test function select(sel) { if (sel) { this.push(query(sel)); } return this; } function query(selector) { let elems = []; var el = doc.querySelectorAll(selector); for (let i = el.length; i--; elems.unshift(el[i])) { }; return elems; } select.prototype = { length: 0, push: function (elems) { [].push.apply(this, elems); return this; }, /* splice: function () { [].splice.apply(this, arguments); }, */ splice: Array.prototype.splice }; window['Q'] = function(selector) { return new select(selector); } })(); const x = Q(".foo"); x.splice(0, 3); for (let n = 0; n < x.length; ++n) { console.log(x[n].textContent); }
 <div class="foo">1</div> <div class="foo">2</div> <div class="foo">3</div> <div class="foo">4</div> <div class="foo">5</div> <div class="foo">6</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM