首页 > 解决方案 > 将对象及其方法传递给另一个函数

问题描述

我有一个导入到我的应用程序中的 npm 库,见下文:

import Chess from 'chess.js';

let chess = new Chess();

以下链接中显示了一种使用方法chess.moves()

https://github.com/jhlywa/chess.js/blob/master/README.md

上面两行代码在我的文件 App.js 中。我有一个实用程序.js 文件,我想在其中放置一个函数,该函数是从 App.js 中的函数调用的。但是,我希望能够chess.moves()在utilities.js 中的函数中使用。

如何使用chess.moves()utilities.js 的内部?

标签: reactjs

解决方案


在您的导入函数中传递国际象棋对象对您有用吗?

在你的 utilities.js

let myNewFunction = (chess)=>{
chess.moves();
}

export myNewFunction;

在你的 app.js

import Chess from 'chess.js';
import {myNewFunction } from './utilities';


let chess = new Chess();
....

myNewFunction(chess);
....


推荐阅读