<script type="text/javascript">
function addEachMethod(obj,attrName){
obj[attrName] = function(fn) {
var keys = Object.keys(this);
this.fn = fn;
window.newArr = [];
for (var i = 0,len = keys.length; i < len; i++) {
var value = this[keys[i]];
this.fn(value,i,newArr)
}
Object.defineProperty(obj, "fn", {
"enumerable": false
});
};
Object.defineProperty(obj, attrName, {
"enumerable": false
});
}
var arr = [5, 6, 47, 45, 2, 13, 45, 12, 45];
addEachMethod(arr,"f");
var sum = 0;
arr.f(function(ele){
sum += parseInt(ele);
});
console.log(sum)
arr.f(function(ele,index,newArr) {
newArr[index] = ele + 1;
});
console.log(newArr);
var newSum = 0;
addEachMethod(newArr,"z");
newArr.z(function(ele){
newSum += parseInt(ele);
});
console.log(newSum)
</script>