1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| var ArrayWrapper = function (nums) { this.nums = nums; };
ArrayWrapper.prototype.valueOf = function () { if (this.nums.length === 0) { return 0; } return this.nums.reduce((sum, num) => sum + num, 0); };
ArrayWrapper.prototype.toString = function () { return `[${this.nums.join(",")}]`; };
const obj1 = new ArrayWrapper([1, 2]); const obj2 = new ArrayWrapper([3, 4]);
console.log(obj1 + obj2); console.log(String(obj1));
const obj3 = new ArrayWrapper([23, 98, 42, 70]); console.log(String(obj3));
const obj4 = new ArrayWrapper([]); const obj5 = new ArrayWrapper([]); console.log(obj4 + obj5);
|