我不太清楚你所说的“保守”是什么意思,但我想添加到Array的原型应该比创建自定义对象更快,你只需要实现你想要的方法。
Array.prototype._interval_plus = function (arr2) {
return [this[0] + arr2[0], this[1] + arr2[1]];
};
Array.prototype._interval_minus = function (arr2) {
return [this[0] - arr2[0], this[1] - arr2[1]];
};
Array.prototype._interval_multiply = function (arr2) {
var ac = this[0] * arr2[0],
ad = this[0] * arr2[1],
bc = this[1] * arr2[0],
bd = this[1] * arr2[1];
return [Math.min(ac, ad, bc, bd), Math.max(ac, ad, bc, bd)];
};
Array.prototype._interval_divide = function (arr2) {
var ac, ad, bc, bd;
if (arr2[0] === 0 || arr2[1] === 0)
throw new Error('division by zero');
ac = this[0] / arr2[0],
ad = this[0] / arr2[1],
bc = this[1] / arr2[0],
bd = this[1] / arr2[1];
return [Math.min(ac, ad, bc, bd), Math.max(ac, ad, bc, bd)];
};
Array.prototype._interval_pow = function (arr2, pow) {
var ac = this[0] * Math.pow(arr2[0], pow),
ad = this[0] * Math.pow(arr2[1], pow),
bc = this[1] * Math.pow(arr2[0], pow),
bd = this[1] * Math.pow(arr2[1], pow);
return [Math.min(ac, ad, bc, bd), Math.max(ac, ad, bc, bd)];
};
现在你可以做类似的事情
var x = [ 0 , 1 ];
y = [ 0.5, 3.5];
x._interval_plus(y) // [ 0.5 , 4.5 ]
._interval_multiply(y) // [ 0.25, 15.75]
._interval_minus(y) // [-0.25, 12.25]
._interval_pow(y, 2); // [-3.0625, 150.0625]