如何在我自己的数组原型函数中使用数组函数
How to use Array functions inside my own Array prototype function?
我第一次尝试写一个数组原型函数
原文是这样的,
-
array is [2,0,1,3]
-
返回30102,基本上将数组反转为[3,1,0,2]
-
then 3*1000000 + 1 *10000 + 0*100 + 2
所以我想实现一个数组函数来做这个
Array.prototype.blobArray2Int
= Array.prototype.blobArray2Int || function() {
//Array.prototype.reverse();
Array.prototype = Array.prototype.reverse();
cnt = Array.prototype.reduce(function(total, num) {
return total*100 + num;
});
return cnt;
}
问题是,当我真正使用它时,实现内部的Array变为空,(当我使用blobArray2Int()方法时,我确实打印了数组)。
请问如何修理它?谢谢!
您应该将您的数组称为this
而不是Array.prototype
。所以你的代码应该看起来像这样:
var a = new Array(2, 0, 1, 3);
Array.prototype.blobArray2Int = Array.prototype.blobArray2Int || function() {
return this.reduceRight(function(total, num) {
return total * 100 + num;
});
};
document.write(a.blobArray2Int());
相关文章:
- 使用jQuery浏览函数数组
- coffescript/javascript函数数组的行为真的很奇怪
- 返回多个变量的 JavaScript 函数:数组与对象.最佳实践是什么
- 如何在 JavaScript 中动态地编写可迭代的函数数组
- JavaScript 自包含函数数组访问自我索引
- 如何在javascript中调用函数数组的索引号
- 从外部函数(数组)了解返回函数(x)
- Javascript函数数组
- 引用函数数组中的另一个函数
- 文本框搜索/Javascript函数数组**不对应**
- 返回一个函数数组
- selenium-webdriver 将函数数组作为参数传递给 executeScript
- 70%的时间运行JavaScript函数,从函数数组中随机选择
- 如何循环函数数组并在单击时一次执行一个
- 如何在 Javascript 命名空间脚本中创建函数数组
- 创建函数数组
- 在 Jscript 中为 Q.all() 构建一个动态函数数组
- 打印函数数组的函数名称
- 循环遍历函数数组 - 对象不支持此属性或方法 (IE8)
- 将函数数组传递给另一个函数