【问题标题】:Storing reference of a class instance member function makes it loose scope存储类实例成员函数的引用使其范围松散
【发布时间】:2017-07-05 10:19:08
【问题描述】:

我正在使用 Node v8.1.3

我在文件utility.js 中有一个课程Utility

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

为什么会出现此错误,我该如何解决?谢谢!

【问题讨论】:

    标签: node.js class scope ecmascript-6 this


    【解决方案1】:

    使用bind函数绑定上下文:

    constructor() {
        this.typeChecker = require('javascript-type-checker');
        this.internalErrors = require('../constants/internalErrors');
        this.axios = require('axios');
        this.config = require('../config');
        this.getCurrentWeatherByLatLong = this.getCurrentWeatherByLatLong.bind(this)
    }
    

    this 指向调用函数的对象。所以,当你打电话给utility.getCurrentWeatherByLatLong(...) 时,this 就是utility。但是,当您调用functionToCall(...) 时,thisundefined

    或者,正如您在 cmets 中建议的那样,您可以将 functionToCall 绑定到 utility

    const utility = require('./utility');
    let functionToCall = utility.getCurrentWeatherByLatLong;
    functionToCall = functionToCall.bind(utility);
    functionToCall(Number(latitude), Number(longitude)).then((result) => {
        console.log(result);
    })
    

    【讨论】:

    • 好吧,这行得通。为什么它会松动范围?我不应该将functionToCall绑定到utility吗?
    • @AyushGupta 我已经用更好的解释更新了我的答案。希望这能回答你的问题:)
    • 谢谢!但问题是,当我执行 functionToCall = functionToCall.bind(utility); 时,我再次收到该错误
    • @AyushGupta 嗯,这很奇怪。我刚刚在一个小得多的示例应用程序上对其进行了测试,并且该逻辑似乎有效。您是否遇到与以前完全相同的错误?
    • 抱歉,回复晚了,但是是的。不过我会再试一次
    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 2018-11-16
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    相关资源
    最近更新 更多