0
我導出了一個類來表示網格中的一個點。這個類有一個「距離」方法。NodeJS:導出的類有一個返回undefined的方法
當我在另一個nodeJs文件中使用此方法時,結果始終爲Undefined。 我不明白爲什麼。
這裏是我的源代碼:
const math = require('Math')
function checkCoordinate(x) {
if ((typeof x == 'number') &&
(x > 0) && (x % 1 == 0)) {
return x
}
else throw (new Error('ERR_INVALID_ARG_TYPE'))
}
/**
* Build a new square for a grid
* @class
* @classdesc Represent a square in a grid
*/
class square {
constructor (abscissa, ordinate) {
try {
this.abscissa = checkCoordinate(abscissa)
this.ordinate = checkCoordinate(ordinate)
} catch (err) {
throw (err)}
}
distance (square2) {
if (square2 instanceof square)
return
math.ceil (
math.sqrt(
math.pow((this.abscissa-square2.abscissa), 2) +
math.pow((this.ordinate-square2.ordinate), 2)
)
)
throw (new Error('ERR_INVALID_ARG_TYPE'))
}
}
module.exports = square
當我嘗試使用它:
var Square = require ('./objects/square.js')
var sA = new Square(1,1)
console.log(sA.distance(new Square(20,20)))
,結果是未定義:
$ npm start
> [email protected] start D:\Documents\Programmation\NodeJS\target-rpg
> node server.js
undefined
我應該得到27代替我得到了未定義。 我無法弄清楚。
我正在使用節點8.1.4
任何幫助嗎?
哦。這證明我的愚蠢沒有限制。 謝謝穆克什 –