admin管理员组文章数量:1327464
If I have a constructor and want to sum the parameter values and output to an inner method, I thought I could do the following:
function Stats(a, b, c, d, e, f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
var total = 0;
var array = [a, b, c, d, e, f];
var len = array.length;
this.sum = function() {
for(var i = 0; i < len; i++) {
total += array[i];
}
return total;
};
}
var output = new Stats(10, 25, 5, 84, 8, 44);
console.log(output);
When looking at the console 'total' is 0.
I'm sure I have pletely failed with my logic, so if you have suggestions how to improve this (as well as the sum) I'd love to read them.
If I have a constructor and want to sum the parameter values and output to an inner method, I thought I could do the following:
function Stats(a, b, c, d, e, f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
var total = 0;
var array = [a, b, c, d, e, f];
var len = array.length;
this.sum = function() {
for(var i = 0; i < len; i++) {
total += array[i];
}
return total;
};
}
var output = new Stats(10, 25, 5, 84, 8, 44);
console.log(output);
When looking at the console 'total' is 0.
I'm sure I have pletely failed with my logic, so if you have suggestions how to improve this (as well as the sum) I'd love to read them.
Share Improve this question edited Nov 28, 2013 at 18:22 Anto Jurković 11.3k2 gold badges30 silver badges42 bronze badges asked Apr 18, 2013 at 22:48 psdpainterpsdpainter 6661 gold badge11 silver badges28 bronze badges 1-
var total
is declared outside thesum
method so the first time you callsum
you will get the correct value, and the next time you callsum
you will get double the correct value. – Mike Samuel Commented Apr 18, 2013 at 22:54
6 Answers
Reset to default 3This can be abbreviated.
function Stats(var_args) {
var sum = 0;
// The arguments pseudo-array allows access to all the parameters.
for (var i = 0, n = arguments.length; i < n; ++i) {
// Use prefix + to coerce to a number so that += doesn't do
// string concatenation.
sum += +arguments[i];
}
// Set the sum property to be the value instead of a method
// that putes the value.
this.sum = sum;
}
var output = new Stats(10, 25, 5, 84, 8, 44);
// You can use a format string to see the object and a specific value.
console.log("output=%o, sum=%d", output, output.sum);
function Stats(){
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
The Arguments variable contains all arguments of the function in an array.
Not sure what you wanted to achieve there but I thought it might be useful looking at your variable stack there
Available on jsfiddle
function Stats(a, b, c, d, e, f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.sum = Array.prototype.reduce.call(arguments, function (x, y) {
return x + y;
}, 0);
}
var output = new Stats(10, 25, 5, 84, 8, 44);
console.log(output);
You have to call sum - output is the object:
console.log(output.sum());
and to improve your class i would go on something more general to not limit the num of my params if all i want to do is to sum them:
function Stats() {
this.total = (function(args){
var total = 0;
for(var i = 0; i < args.length; i++) {
total += args[i];
}
return total;
})(arguments);
}
var output = new Stats(10, 10, 5, 10, 10, 10,100,24,1000);
console.log(output.total);
Optimized version that, I think, does what you wanted:
function Stats() {
var _arguments = arguments;
this.sum = function() {
var i = _arguments.length;
var result = 0;
while (i--) {
result += _arguments[i];
}
return result;
};
}
var output = new Stats(10, 25, 5, 84, 8, 44);
console.log(output.sum());
Based on how you have written the code, you should be doing
console.log(output.sum());
In order to get the desired output
本文标签: Sum the parameters in javascript constructor and return totalStack Overflow
版权声明:本文标题:Sum the parameters in javascript constructor and return total - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742210503a2433624.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论