1
我使用节点v8.1.3存储基准使其松动范围
我在文件中有一类Utility
现在utility.js
class Utility {
constructor() {
this.typeChecker = require('javascript-type-checker');
this.internalErrors = require('../constants/internalErrors');
this.axios = require('axios');
this.config = require('../config');
}
getCurrentWeatherByLatLong(latitude, longitude) {
if(!this.isValidLatitude(latitude)) throw this.internalErrors.ERR_LAT_INVALID;
if(!this.isValidLongitude(longitude)) throw this.internalErrors.ERR_LONG_INVALID;
const url = `${this.config.BASE_URL}?appid=${this.config.API_KEY}&lat=${latitude}&lon=${longitude}`;
return this.axios.default.get(url);
}
isValidLatitude(latitude) {
return (this.typeChecker.isNumber(latitude) && latitude >= -90 && latitude <=90);
}
isValidLongitude(longitude) {
return (this.typeChecker.isNumber(longitude) && longitude >= -180 && longitude <= 180);
}
}
module.exports = new Utility();
,我在其他文件中,当我做
const utility = require('./utility');
utility.getCurrentWeatherByLatLong(Number(latitude), Number(longitude))
.then((result) => {
console.log(result)
})
它工作正常。但是,当我做
const utility = require('./utility');
const functionToCall = utility.getCurrentWeatherByLatLong;
functionToCall(Number(latitude), Number(longitude))
.then((result) => {
console.log(result)
})
我得到的错误:Cannot read property 'isValidLatitude' of undefined
。
为什么会发生此错误,我该如何解决它?谢谢!
好奏效。为什么它放宽了范围呢?并且不应该将'functionToCall'绑定到'utility'? –
@AyushGupta我用更好的解释更新了我的答案。希望能回答你的问题:) – dan
谢谢!但事情是,当我做'functionToCall = functionToCall.bind(工具);'我再次得到该错误 –