首页 > 解决方案 > 一个 var 怎么能有一个值,然后在同一个 console.log 消息中被取消分配?

问题描述

我可能遗漏了一些明显的东西,我有一个分配给this.theValue. 立即记录此值表明它this.theValue具有价值。后来这个值神奇地未定义。但是,如果我记录以下内容console.log(this, this.theValue),则第一个显示 theValue 存在,但 this.theValue 未定义。

在此处输入图像描述

这是它的样子:

/*global define, window */
define([
    'competitor/show/CompetitorLabelColours',
    'competitor/show/CompetitorLabelText'
], function(
    CompetitorLabelColours,
    CompetitorLabelText
) {
    'use strict';

    let CompetitorLabelFactory = function(parameterSet) {
        this.parameterSet = parameterSet;

        this.labeltext = new CompetitorLabelText(parameterSet);
        this.labelColours = new CompetitorLabelColours(parameterSet);

        console.log(this, this.labelText); //here none of them is undefined

        this.scale = 0.5;
        this.scaleLabel = 0.5;
        this.opacity = 0.8;
    };

    ...

    CompetitorLabelFactory.prototype.update = function (competitor, entity) {
        return {
            ...
            labelTextRefresh: () => {
                console.log(this, this.labelText); // here this.labelText is undefined
                entity.label.text = this.labelText.updateText(competitor);
            }
            ...
        }
    };

    ...

    return CompetitorLabelFactory;
});

有什么我想念的想法吗?

我已经搜索了我的整个项目,并且这个变量永远不会重新分配。

标签: javascript

解决方案


我在 的定义中看到了大小写差异labelText,您为 分配了一个值labeltext,所以undefined当您记录它时


推荐阅读