最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sum the parameters in javascript constructor and return total - Stack Overflow

programmeradmin5浏览0评论

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 the sum method so the first time you call sum you will get the correct value, and the next time you call sum you will get double the correct value. – Mike Samuel Commented Apr 18, 2013 at 22:54
Add a ment  | 

6 Answers 6

Reset to default 3

This 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

发布评论

评论列表(0)

  1. 暂无评论