传递函数时 arguments[] 中的意外变量
Unexpected variables in arguments[] when passing a function
我在一个文件夹中有两个文件 - index.js
和 util.js
其代码库如下
Util.js
let obj = {}
obj.sendTransaction = () => {
console.log(arguments);
return new Promise((resolve, reject) => {
// try {
// let data = ethFunction.call()
// resolve(data)
// } catch (e) {
// reject(e)
// }
});
}
module.exports = obj
在 Index.js
中,如果我将参数传递给 addNewParticipant
或其变体,那么它们不会出现在 util.js
的参数对象中,例如
const addNewParticipant = (foo, bar) => {
var ethFunction = myContract.addParticipant.sendTransaction
console.log(ethFunction);
EthUtil.sendTransaction()
}
const addNewParticipantTwo = (foo, bar) => {
var ethFunction = myContract.addParticipant.sendTransaction
console.log(ethFunction);
EthUtil.sendTransaction(ethFunction, foo, bar)
}
并这样称呼它 addNewParticpant(1, 2)
和 addNewParticpantNew(1, 2)
数字 1 和 2 不会出现在 util 函数的参数对象中。事实上,arguments 对象保持不变,4 个输入描述 node_modules
中的一些函数和文件,包括 Bluebird
和对 index.js
本身的引用
我的最终目标是
将函数从 index.js
传递到 util.js
传递未知数量的变量
调用传递的函数并对其应用未知数量的变量
将整个事情包装在一个承诺中并进行一些数据验证
理想情况下 arguments[0]
代表我要传递的一个函数,另一个是值。然后我会使用
var result = arguments[0].apply(null, Array().slice.call(arguments, 1));
如果有帮助,我想传递的函数有一个可选的回调功能
正如评论中已经提到的,粗箭头没有自己的 this
或 arguments
对象。您正在记录的 arguments
对象来自模块加载器创建的函数及其传递的参数。
您可以使用 "regular function",或者在这种情况下,您可以使用 ...rest parameter
并且,避免延迟反模式。
//first a little utility that might be handy in different places:
//casts/converts a value to a promise,
//unlike Promise.resolve, passed functions are executed
var promise = function(value){
return typeof value === "function"?
this.then( value ):
Promise.resolve( value );
}.bind( Promise.resolve() );
module.exports = {
sendTransaction(fn, ...args){
return promise(() => fn.apply(null, args));
}
}
我在一个文件夹中有两个文件 - index.js
和 util.js
其代码库如下
Util.js
let obj = {}
obj.sendTransaction = () => {
console.log(arguments);
return new Promise((resolve, reject) => {
// try {
// let data = ethFunction.call()
// resolve(data)
// } catch (e) {
// reject(e)
// }
});
}
module.exports = obj
在 Index.js
中,如果我将参数传递给 addNewParticipant
或其变体,那么它们不会出现在 util.js
的参数对象中,例如
const addNewParticipant = (foo, bar) => {
var ethFunction = myContract.addParticipant.sendTransaction
console.log(ethFunction);
EthUtil.sendTransaction()
}
const addNewParticipantTwo = (foo, bar) => {
var ethFunction = myContract.addParticipant.sendTransaction
console.log(ethFunction);
EthUtil.sendTransaction(ethFunction, foo, bar)
}
并这样称呼它 addNewParticpant(1, 2)
和 addNewParticpantNew(1, 2)
数字 1 和 2 不会出现在 util 函数的参数对象中。事实上,arguments 对象保持不变,4 个输入描述 node_modules
中的一些函数和文件,包括 Bluebird
和对 index.js
本身的引用
我的最终目标是
将函数从
index.js
传递到util.js
传递未知数量的变量
调用传递的函数并对其应用未知数量的变量
将整个事情包装在一个承诺中并进行一些数据验证
理想情况下 arguments[0]
代表我要传递的一个函数,另一个是值。然后我会使用
var result = arguments[0].apply(null, Array().slice.call(arguments, 1));
如果有帮助,我想传递的函数有一个可选的回调功能
正如评论中已经提到的,粗箭头没有自己的 this
或 arguments
对象。您正在记录的 arguments
对象来自模块加载器创建的函数及其传递的参数。
您可以使用 "regular function",或者在这种情况下,您可以使用 ...rest parameter
并且,避免延迟反模式。
//first a little utility that might be handy in different places:
//casts/converts a value to a promise,
//unlike Promise.resolve, passed functions are executed
var promise = function(value){
return typeof value === "function"?
this.then( value ):
Promise.resolve( value );
}.bind( Promise.resolve() );
module.exports = {
sendTransaction(fn, ...args){
return promise(() => fn.apply(null, args));
}
}