简体   繁体   English

JavaScript用作带参数的参数

[英]Javascript functions as Arguments with Arguments

What I want is to load multiple files. 我想要的是加载多个文件。 These files all use the same function to be loaded, only thing different is the file name. 这些文件都使用相同的功能加载,唯一不同的是文件名。 This function should return an object. 此函数应返回一个对象。 Something like this: 像这样:

var files = [ loadFile("file1.txt"), loadFile("file2.txt"), loadFile("file3.txt") ]; 

// Example function
function loadFile( file_name ) {
    return "loaded file " + file_name;
}

But when I run this it loads it directly. 但是,当我运行它时,它将直接加载它。

var files = [ loadFile, loadFile, loadFile ];

// now please
for (var i = 0; i < files.length; i++) {
    files[i]();
}

But this way I can't give it arguments. 但是这样我不能给它参数。 Now I can create a filler function like this, but I there is probably a better way to do this... 现在我可以创建这样的填充函数,但是我可能有更好的方法来执行此操作...

function loadFile1() {
    return loadFile( "file1.txt" );
}

If it is possible, how can I load Javascript functions as Arguments with Arguments? 如果可能,如何将Javascript函数作为带参数的参数加载?

*done some testing. *做了一些测试。 I am going with bind() for the answers, as that is what I was looking for. 我将使用bind()作为答案,因为这就是我想要的。 But I want to mention Stuart's answer that I definitely will keep in mind for the future use. 但我想提一下Stuart的答案,我一定会牢记将来的用途。 MoeSattler & vlaz thank you guys for showing other good ways of doing this! MoeSattlervlaz谢谢你们展示了其他好的方法!

Cant you do: 不能这样做:

// Example function
function loadFile( file_name ) {
    return "loaded file " + file_name;
}

var files = ["file1.txt", "file2.txt", "file3.txt"]; 
for (i = 0; i < files.length; i++) {
    loadFile(files[i]);
}

How about map ? map怎么样?

 function loadFile( file_name ) { return "loaded file " + file_name; } const fileNames = ['file1.txt', 'file2.txt', 'file3.txt'] const files = fileNames.map(loadFile) console.log(files) 

You could use Function#bind for binding the parameter to the function and call later without parameter. 您可以使用Function#bind将参数绑定到函数,然后在不使用参数的情况下调用。

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. bind()方法创建一个新的功能,调用它时,具有其this关键字设置为所提供的值,与前述的当新功能被调用任何设置参数给定的序列。

 function loadFile( file_name ) { return "loaded file " + file_name; } var files = [ loadFile.bind(null, "file1.txt"), loadFile.bind(null, "file2.txt"), loadFile.bind(null, "file3.txt") ]; files.forEach(function (a) { console.log(a()); }); 

Option 1 is to use what is called a thunk - it's simply a function that does not take any arguments and returns a value when executed. 选项1使用所谓的thunk-它只是一个不带任何参数并在执行时返回值的函数。 It's useful for returning something at a later point. 这对于以后返回一些东西很有用。 You actually do have an example of that with loadFile1 but you can generalise it. 实际上,您确实具有loadFile1的示例,但是可以对其进行概括。

function loadFileLater(fileName) {
    return function() {
        loadFile( fileName );
    }
}

Or using Function.prototype.bind() 或使用Function.prototype.bind()

function loadFileLater(fileName) {
    return loadFile.bind(null, fileName);
}

Alternatively, you can flip it around and instead of having an array of functions that need values, you can have an array of values where you execute a function against each 另外,您可以翻转它,而不是拥有需要值的函数数组,而可以拥有一个数组,在其中对每个对象执行一个函数

 function loadFile( file_name ) { return "loaded file " + file_name; } var filesNames = [ "file1.txt", "file2.txt", "file3.txt", "file4.txt" ] var files = filesNames.map(loadFile) console.log(files); 

In your case, you are most likely going to use .forEach rather than .map but it's the exact same principle. 在您的情况下,您很可能会使用.forEach而不是.map但这是完全相同的原理。

bind is going to be your best friend here. bind将成为您最好的朋友。

Typically bind is used to assign a context to a function (ie: var myBoundFunc = myFunc.bind(myObj) will make sure that any references to this in MyFunc when called via myBoundFunc will point to myObj ), but bind has the added bonus of also being able to store parameters to call myFunc with. 通常, bind用于为函数分配上下文(即: var myBoundFunc = myFunc.bind(myObj)将确保通过myBoundFunc调用MyFunc this任何引用都指向myObj ),但是bind具有以下优点:还能够存储参数来调用myFunc

So in your case: 因此,在您的情况下:

var files = [ loadFile.bind(this, "file1.txt"), loadFile.bind(this, "file2.txt"), loadFile.bind(this, "file3.txt") ]; 

Where this can be any context, and every argument after is used when the resulting function (ie files[0]() ) is called. 其中this可以是任何上下文,并且当得到的函数(即,每一个参数,则使用后files[0]()被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM