Why does Array.prototype.push return the new length instead of something more useful?

后端 未结 4 1877
悲哀的现实
悲哀的现实 2020-12-15 19:49

Ever since its introduction in ECMA-262, 3rd Edition, the Array.prototype.push method\'s return value is a Number:

4条回答
  •  盖世英雄少女心
    2020-12-15 20:09

    I posted this in TC39's communication hub, and was able to learn a bit more about the history behind this:

    push, pop, shift, unshift were originally added to JS1.2 (Netscape 4) in 1997.

    There were modeled after the similarly named functions in Perl.

    JS1.2 push followed the Perl 4 convention of returning the last item pushed. In JS1.3 (Netscape 4.06 summer 1998) changed push to follow the Perl 5 conventions of returning the new length of the array.

    see https://dxr.mozilla.org/classic/source/js/src/jsarray.c#804

    /*
     * If JS1.2, follow Perl4 by returning the last thing pushed.  Otherwise,
     * return the new array length.
     */
    

提交回复
热议问题