Home > Net >  Javascript TypeError: undefined is not a constructor (evaluating 'new _Team.GameTeam('name
Javascript TypeError: undefined is not a constructor (evaluating 'new _Team.GameTeam('name

Time:10-19

I keep getting a TypeError whenever I attempt to compile in React Native. Here is the class I'm trying to create an instance of:

class GameTeam {
    constructor(name, logoLocation)
    {
        this.name = name;
        this.logoLocation = logoLocation;
    };

    getTeamName() {
        return this.name;
    }

    getTeamLogoLocation() {
        return this.logoLocation
    }
}

and I'm just creating with:

const home = new GameTeam('name', 'null');

I've also tried change const to var and that hasn't done anything.

If it helps solve the issue at all, here's the main fileL

import { GameTeam } from './objects/Team'
import React from 'react';
import { Image, StyleSheet, Text, View } from 'react-native';
import { useFonts } from 'expo-font'
import { VersusScore } from './components/VersusScore';

export default function App() {
  const home = new GameTeam('Tigers', 'null');
  const away = new GameTeam('Raiders', 'null');
  return (
    <View style={mainStyles.container}>
      <View style={mainStyles.topBar}>
        <Image style={mainStyles.navIcon_small} source={require('./assets/Icons/Inactive/Settings_Icon.png')} />
        <Text style={mainStyles.Title}>BRC</Text>
        <Image style={mainStyles.navIcon_small} source={require('./assets/Icons/Inactive/Announcements_Icon.png')} />
      </View>
      <VersusScore homeTName={home} awayTName={away} />
    </View>
  );
}

CodePudding user response:

Only difference I have seen with this one and the docs is that there is a ; at the end of constructor which might not give syntax error might also block it from working

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

CodePudding user response:

Turns out I needed to change class GameTeam to export default class GameTeam in the first file, since it was a separate file, now it looks like this:

export default class GameTeam {
    constructor(name, logoLocation)
    {
        this.name = name;
        this.logoLocation = logoLocation;
    }

    getTeamName() {
        return this.name;
    }

    getTeamLogoLocation() {
        return this.logoLocation
    }
}
  • Related