javascript - “this”指针未定义可能是因为它丢失了,但是怎么办呢?

标签 javascript react-native

我收到此消息:

TypeError: this.getRandomQuestion is not a function. (In 'this.getRandomQuestion()', 'this.getRandomQuestion' is undefined).

我几乎可以肯定这是因为“this”指针没有一直到达我的回调(getNext() 请参阅注释)。它早些时候可以工作,但我需要添加另一层间接来混合我的测验中的答案。从那时起,它开始失败。

似乎也设置了绑定(bind)。

'use strict';
import Colors from './Colors';
import type {Node} from 'react';
import {Text, Button,View,StyleSheet, ImageBackground} from 'react-native';
import React, {Component} from 'react';
import * as data from './data/questions.json';

export default class Question extends React.Component {
    constructor(props) {
        super(props);
        this.state = {thisQuestion: this.getRandomQuestion()};

        this.getRandomQuestion = this.getRandomQuestion.bind(this);
        this.getNext = this.getNext.bind(this);     
    }

    noOp() {
        return;
    }

    getRandomQuestion() {
        var questionCount = data.Questions.length;
        var randomIndex = Math.floor((Math.random() * questionCount));
        var newQ = data.Questions[randomIndex];

        var offset = Math.floor((Math.random() * 4));

        if (offset == 0) {
            newQ.a1 = newQ.Answer1;
            newQ.a2 = newQ.Answer2;
            newQ.a3 = newQ.Answer3;
            newQ.a4 = newQ.Answer4;
            newQ.click1 = this.getNext;
            newQ.click2 = this.noOp;
            newQ.click3 = this.noOp;
            newQ.click4 = this.noOp;            
        }

        if (offset == 1) {
            newQ.a4 = newQ.Answer1;
            newQ.a1 = newQ.Answer2;
            newQ.a2 = newQ.Answer3;
            newQ.a3 = newQ.Answer4;

            newQ.click4 = this.getNext;
            newQ.click1 = this.noOp;
            newQ.click2 = this.noOp;
            newQ.click3 = this.noOp;            
        }

        if (offset == 2) {
            newQ.a3 = newQ.Answer1;
            newQ.a4 = newQ.Answer2;
            newQ.a1 = newQ.Answer3;
            newQ.a2 = newQ.Answer4;
            newQ.click3 = this.getNext;
            newQ.click4 = this.noOp;
            newQ.click1 = this.noOp;
            newQ.click2 = this.noOp;            
        }

        if (offset == 3) {
            newQ.a2 = newQ.Answer1;
            newQ.a3 = newQ.Answer2;
            newQ.a4 = newQ.Answer3;
            newQ.a1 = newQ.Answer4;
            newQ.click2 = this.getNext;
            newQ.click3 = this.noOp;
            newQ.click4 = this.noOp;
            newQ.click1 = this.noOp;            
        }

        return newQ;
    }

    getNext() {
        alert('in getNext ' + this.getRandomQuestion);  // <--- this shows "in getNext undefined"
        var newQ = this.getRandomQuestion();    // this is where it fails ... not defined. Huh?
        this.setState( {thisQuestion:newQ} );       
    }

    render() {
        var thisQuestion = this.state.thisQuestion;

        return <View style={styles.body}>
            <View style={styles.sectionContainer}>
                <Text style={styles.sectionTitle}>{thisQuestion.Text}</Text>
                <View style={styles.sectionContainer}>
                    <Button title={thisQuestion.a1} color="gray" onPress={() => thisQuestion.click1() }/>   
                </View>
                <View style={styles.sectionContainer}>
                    <Button title={thisQuestion.a2} color="gray" onPress={() => thisQuestion.click2() }/>  
                </View>
                <View style={styles.sectionContainer}>
                    <Button title={thisQuestion.a3} color="gray" onPress={() => thisQuestion.click3() }/>  
                </View>
                <View style={styles.sectionContainer}>
                    <Button title={thisQuestion.a4} color="gray" onPress={() => thisQuestion.click4() }/>  
                </View>
            </View>
        </View>;
    }
}

如何获取指向我的回调的“this”指针?

编辑:添加了包含数据信息的行(它来自 json 文件)。

最佳答案

我认为您可以直接将监听器与绑定(bind)添加到元素,而不是在构造函数中将函数设置为同一函数的绑定(bind)版本,例如:

newQ.click1 = this.getNext.bind(this) 

并对您可能拥有的其他听众执行相同的操作。

至少我更喜欢这样做,此外,如果由于某种原因您需要向监听器添加不同的绑定(bind)范围(例如,不使用 this),您可以通过仅调整特定元素/监听器的绑定(bind)。

关于javascript - “this”指针未定义可能是因为它丢失了,但是怎么办呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59842349/

相关文章:

javascript - 循环声音onclick只是

javascript - ng-show 令人不安的 div 布局 - angularJS

javascript - 在 angularjs 中显示/隐藏模板 URL

react-native - 世博会 - 将方向锁定为横向

javascript - 使用 jquery/js 将 html 元素显示为带有暂停的序列

javascript - 打破 PrototypeJS .each() 循环

javascript - 更新 expo : Element type is invalid: expected a string (for built-in components) or a class/function 后

ios - 找不到 React Native RTCAnimation

javascript - react native 。 MP3 二进制字符串 (Uint8Array(9549)) 到流或文件

javascript - “the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded” 来自 Android React Native 中的 super 代理