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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值