乘法时错误的 return 值
Wrong return value when multiplying
console.log()
的输出是我用getRatioValue()
函数将height
的插入值乘以计算出的比率时width
的值。我找不到为什么会这样。
var soFunction = function(args) {
this.width = args.width || 0;
this.height = args.height || 0;
this.getRatioValue = function(value) {
var ratio = this.width / this.height;
return value * ratio;
};
console.log(this.getRatioValue(this.height)); // returns 1200
}
// Initialize object
var test = new soFunction({width: 1200, height: 980});
这是一个简单的数学运算。如果您将函数写在一行上,您将得到。
value
等于height
,所以
return this.height * this.width / this.height
this.height
自相残杀 returns this.width
console.log()
的输出是我用getRatioValue()
函数将height
的插入值乘以计算出的比率时width
的值。我找不到为什么会这样。
var soFunction = function(args) {
this.width = args.width || 0;
this.height = args.height || 0;
this.getRatioValue = function(value) {
var ratio = this.width / this.height;
return value * ratio;
};
console.log(this.getRatioValue(this.height)); // returns 1200
}
// Initialize object
var test = new soFunction({width: 1200, height: 980});
这是一个简单的数学运算。如果您将函数写在一行上,您将得到。
value
等于height
,所以
return this.height * this.width / this.height
this.height
自相残杀 returns this.width