gpt4 book ai didi

javascript - 函数外的 'return' 问题

转载 作者:行者123 更新时间:2023-11-30 08:17:42 24 4
gpt4 key购买 nike

我目前正在尝试开发一个使用 ES6 语法的系统,尽管在编译时出现错误

GameSettingsStore.js: 'return' outside of function

我得到的当前代码是:

文件 index.js

import GameSettingsStore from '../stores/GameSettingsStore';

文件 GameSettingsStore.js

// Packages
import _ from 'lodash';

// Modules
import AppDispatcher from '../dispatcher/AppDispatcher';
import { Constants } from '../constants/AppConstants';
import * as Events from '../lib/events';
import Clib from '../game-logic/clib';

const CHANGE_EVENT = 'change';

/*
* Display Settings
*/
let _controlsSize = Clib.localOrDef('controlsSize', 'big');
let _graphMode = Clib.localOrDef('graphMode', 'graphics');
let _controlsPosition = Clib.localOrDef('controlsPosition', 'right');
let _leftWidget = Clib.localOrDef('leftWidget', 'players');

/*
* HotKeys
*/
let _hotkeysActive = false;

/*
* Ignore Clients
*/
const _ignoredClientList = JSON.parse(Clib.localOrDef('ignoredList', '{}'));

/*
* Store
*/
const GameSettingsStore = _.extend({}, Events, {

emitChange() {
this.trigger(CHANGE_EVENT);
},

addChangeListener(callback) {
this.on(CHANGE_EVENT, callback);
},

removeChangeListener(callback) {
this.off(CHANGE_EVENT, callback);
},

_setGraphMode(graphMode) {
_graphMode = graphMode;
localStorage.graphMode = graphMode;
},

_setControlsSize(controlsSize) {
_controlsSize = controlsSize;
localStorage.controlsSize = controlsSize;
},

_toggleHotkeysState() {
_hotkeysActive = !_hotkeysActive;
localStorage.hotKeysActive = _hotkeysActive;
},

_ignoreUser(username) {
_ignoredClientList[username.toLowerCase()] = { username };
localStorage.ignoredList = JSON.stringify(_ignoredClientList);
},

_approveUser(username) {
username = username.toLowerCase();
if (_ignoredClientList[username]) {
delete _ignoredClientList[username];
localStorage.ignoredList = JSON.stringify(_ignoredClientList);
}
},

getState() {
return {
graphMode: _graphMode,
controlsSize: _controlsSize,
controlsPosition: _controlsPosition,
leftWidget: _leftWidget,
hotkeysActive: _hotkeysActive
};
},

getIgnoredClientList() {
return _ignoredClientList;
}

});

AppDispatcher.register((payload) => {
const { action } = payload;

switch (action.actionType) {
case Constants.ActionTypes.SET_CONTROLS_SIZE:
GameSettingsStore._setControlsSize(action.controlsSize);
GameSettingsStore.emitChange();
break;

case Constants.ActionTypes.SET_GRAPH_MODE:
GameSettingsStore._setGraphMode(action.graphMode);
GameSettingsStore.emitChange();
break;

case Constants.ActionTypes.TOGGLE_HOYTKEYS_STATE:
GameSettingsStore._toggleHotkeysState();
GameSettingsStore.emitChange();
break;

case Constants.ActionTypes.IGNORE_USER:
GameSettingsStore._ignoreUser(action.username);
GameSettingsStore.emitChange();
break;

case Constants.ActionTypes.APPROVE_USER:
GameSettingsStore._approveUser(action.username);
GameSettingsStore.emitChange();
break;

default:
GameSettingsStore.emitChange();
break;

}
return true;
});

return GameSettingsStore;

我假设错误是因为我没有导出我的函数,虽然我不确定我会怎么做,因为我的AppDispatcher 如果这是问题所在的代码。

最佳答案

exportexport default 而不是 return

关于javascript - 函数外的 'return' 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59330785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com