在JavaScript中实现Array.count()方法

    技术2022-07-11  114

    As much as I write about MooTools on my blog, I spend most of my work day knee-deep in PHP. As you probably know, one way to get the size of an array in PHP is to use the count() function:

    正如我在博客上撰写有关MooTools的文章一样,我大部分时间都是在PHP上度过的。 您可能知道,在PHP中获取数组大小的一种方法是使用count()函数:

    echo count($my_array);

    In JavaScript, the way to get the size of an array is to use the length property, like this:

    在JavaScript中,获取数组大小的方法是使用length属性,如下所示:

    alert(my_array.length);

    For some reason, I absolutely hate the ".length" way of retrieving the length of an array. For this reason, I've implement the count() method into JavaScript:

    由于某种原因,我绝对讨厌检索数组长度的“ .length”方法。 因此,我将count()方法实现到JavaScript中:

    Array.prototype.count = function() { return this.length; };

    The count() habit is difficult to break, so why try?

    count()习惯很难打破,那么为什么要尝试呢?

    翻译自: https://davidwalsh.name/implementing-array-count-method-javascript

    相关资源:数值动画效果countUp.js.zip
    Processed: 0.008, SQL: 9