Javascript TypeError: undefined is not a constructor (evaluating 'new _Team.GameTeam('name', 'null')')
Javascript TypeError: undefined is not a constructor (evaluating 'new _Team.GameTeam('name', 'null')')
每当我尝试在 React Native 中编译时,我总是收到 TypeError。
这是 class 我正在尝试创建一个实例:
class GameTeam {
constructor(name, logoLocation)
{
this.name = name;
this.logoLocation = logoLocation;
};
getTeamName() {
return this.name;
}
getTeamLogoLocation() {
return this.logoLocation
}
}
我只是用:
const home = new GameTeam('name', 'null');
我也尝试过将 const 更改为 var,但没有做任何事情。
如果它有助于解决问题,这里是主文件L
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>
);
}
我看到的这个和文档的唯一区别是有一个;在可能不会给出语法错误的构造函数的末尾也可能会阻止它工作
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
原来我需要改变
class GameTeam
到第一个文件中的 export default class GameTeam
,因为它是一个单独的文件,现在它看起来像这样:
export default class GameTeam {
constructor(name, logoLocation)
{
this.name = name;
this.logoLocation = logoLocation;
}
getTeamName() {
return this.name;
}
getTeamLogoLocation() {
return this.logoLocation
}
}
每当我尝试在 React Native 中编译时,我总是收到 TypeError。 这是 class 我正在尝试创建一个实例:
class GameTeam {
constructor(name, logoLocation)
{
this.name = name;
this.logoLocation = logoLocation;
};
getTeamName() {
return this.name;
}
getTeamLogoLocation() {
return this.logoLocation
}
}
我只是用:
const home = new GameTeam('name', 'null');
我也尝试过将 const 更改为 var,但没有做任何事情。
如果它有助于解决问题,这里是主文件L
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>
);
}
我看到的这个和文档的唯一区别是有一个;在可能不会给出语法错误的构造函数的末尾也可能会阻止它工作
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
原来我需要改变
class GameTeam
到第一个文件中的 export default class GameTeam
,因为它是一个单独的文件,现在它看起来像这样:
export default class GameTeam {
constructor(name, logoLocation)
{
this.name = name;
this.logoLocation = logoLocation;
}
getTeamName() {
return this.name;
}
getTeamLogoLocation() {
return this.logoLocation
}
}