导出的对象无法识别 javascript 中的函数
exported object wont recognize functions in javascript
我有一个使用 Js ES6 创建的模块。
该模块有一个函数调用失败并显示“dictionary.detectLangDictionary 不是一个函数”
当它被定义并且对象在主 js 文件中被识别为对象时,但在调用的函数中没有被识别 - dictionary.detectLangDictionary()
我想了解为什么对象不被识别为对象且函数不被识别。
代码及其结构:
该模块包含 4 种文件类型:
1.the class 防守 - dictionary.js:
export default class Dictionary
{
sourceLanguage;
destinationLanguage;
dictionary;
constructor(sourceLanguage,destinationLanguage,dictionary)
{
this.sourceLanguage=sourceLanguage;
this.destinationLanguage=destinationLanguage;
this.dictionary=dictionary;
}
detectLangDictionary(selectionText) {
...
}
static detectDictionaryFromList(textToDetect,dictionaryList)
{
let detectedDictionary=null;
for (let dictionary in dictionaryList)
{
console.log("current dictionary is:",dictionary)
detectedDictionary=dictionary.detectLangDictionary(textToDetect)
if (detectedDictionary!=null)
{
return detectedDictionary;
}
}
return null;
}
}
2.files 创建字典对象并导出它们 - englishHebrewDictionary.js 和 englishRussionDictionary.js 等:
import Dictionary from "./dictionary.js";
let dict={
...
}
export var EnglishHebrewDictionary=new Dictionary("English","Hebrew",dict);
导出类型 2 的所有文件的累积导出对象 - dictionaries.js:
从“./englishHebrewDictionary.js”导出 *;
从“./englishRussionDictionary.js”导出*;
...
4.the调用一切的主程序:
import * as dictionaries from "./dictionaries/dictionaries.js"
import Dictionary from "./dictionaries/dictionary.js"
/**
*detect which dictionary will fit and what the swaped text should contain
*/
function detectLangDictionary(selectionText) {
console.log("dictionaries value:",dictionaries);
return Dictionary.detectDictionaryFromList(selectionText,dictionaries);
}
错误:dictionary.detectLangDictionary is not a function
日志结果:
主文件 detectLangDictionary 函数将字典识别为应有的对象:
dictionaries value () :
{
EnglishHebrewDictionary: Object { sourceLanguage: "English", destinationLanguage: "Hebrew", dictionary: {…} }
EnglishRussionDictionary: Object { sourceLanguage: "English", destinationLanguage: "Russion", dictionary: {…} }
}
调用的静态目录函数无法识别 EnglishHebrewDictionary 是一个对象:
current dictionary is: EnglishHebrewDictionary
如果我理解你想要做什么,那么看起来字典是一个对象而不是数组。 for 循环遍历 属性 名称,我认为您需要像这样引用它。
dictionaryList[dictionary].detectLangDictionary(textToDetect)
所以在你的代码中它看起来像这样。
static detectDictionaryFromList(textToDetect,dictionaryList)
{
let detectedDictionary=null;
for (let dictionary in dictionaryList)
{
console.log("current dictionary is:",dictionary)
detectedDictionary=dictionaryList[dictionary].detectLangDictionary(textToDetect);
if (detectedDictionary!=null)
{
return detectedDictionary;
}
}
return null;
}
我有一个使用 Js ES6 创建的模块。
该模块有一个函数调用失败并显示“dictionary.detectLangDictionary 不是一个函数”
当它被定义并且对象在主 js 文件中被识别为对象时,但在调用的函数中没有被识别 - dictionary.detectLangDictionary()
我想了解为什么对象不被识别为对象且函数不被识别。
代码及其结构:
该模块包含 4 种文件类型:
1.the class 防守 - dictionary.js:
export default class Dictionary
{
sourceLanguage;
destinationLanguage;
dictionary;
constructor(sourceLanguage,destinationLanguage,dictionary)
{
this.sourceLanguage=sourceLanguage;
this.destinationLanguage=destinationLanguage;
this.dictionary=dictionary;
}
detectLangDictionary(selectionText) {
...
}
static detectDictionaryFromList(textToDetect,dictionaryList)
{
let detectedDictionary=null;
for (let dictionary in dictionaryList)
{
console.log("current dictionary is:",dictionary)
detectedDictionary=dictionary.detectLangDictionary(textToDetect)
if (detectedDictionary!=null)
{
return detectedDictionary;
}
}
return null;
}
}
2.files 创建字典对象并导出它们 - englishHebrewDictionary.js 和 englishRussionDictionary.js 等:
import Dictionary from "./dictionary.js";
let dict={
...
}
export var EnglishHebrewDictionary=new Dictionary("English","Hebrew",dict);
导出类型 2 的所有文件的累积导出对象 - dictionaries.js:
从“./englishHebrewDictionary.js”导出 *; 从“./englishRussionDictionary.js”导出*; ...
4.the调用一切的主程序:
import * as dictionaries from "./dictionaries/dictionaries.js"
import Dictionary from "./dictionaries/dictionary.js"
/**
*detect which dictionary will fit and what the swaped text should contain
*/
function detectLangDictionary(selectionText) {
console.log("dictionaries value:",dictionaries);
return Dictionary.detectDictionaryFromList(selectionText,dictionaries);
}
错误:dictionary.detectLangDictionary is not a function
日志结果:
主文件 detectLangDictionary 函数将字典识别为应有的对象:
dictionaries value () :
{
EnglishHebrewDictionary: Object { sourceLanguage: "English", destinationLanguage: "Hebrew", dictionary: {…} }
EnglishRussionDictionary: Object { sourceLanguage: "English", destinationLanguage: "Russion", dictionary: {…} }
}
调用的静态目录函数无法识别 EnglishHebrewDictionary 是一个对象:
current dictionary is: EnglishHebrewDictionary
如果我理解你想要做什么,那么看起来字典是一个对象而不是数组。 for 循环遍历 属性 名称,我认为您需要像这样引用它。
dictionaryList[dictionary].detectLangDictionary(textToDetect)
所以在你的代码中它看起来像这样。
static detectDictionaryFromList(textToDetect,dictionaryList)
{
let detectedDictionary=null;
for (let dictionary in dictionaryList)
{
console.log("current dictionary is:",dictionary)
detectedDictionary=dictionaryList[dictionary].detectLangDictionary(textToDetect);
if (detectedDictionary!=null)
{
return detectedDictionary;
}
}
return null;
}