简体   繁体   English

使用设置的参数将已定义的函数分配给变量

[英]Assign an already-defined function to a variable with set arguments

I'd like something equivalent to this code, except I don't want my_func to be called when my_var is defined. 我想要与该代码等效的东西,除了我不希望在定义my_var时调用my_func。 I want to call my_var() later on in the code with the original argument ('called!') preserved. 我想稍后在代码中调用my_var()并保留原始参数(“被叫!”)。

function my_func(first_arg) {
alert(first_arg);
};
var my_var = my_func('called!');

How? 怎么样?

Your function will be called when the variable is initialized (and the variable will then hold the output of your function, which isn't what you want). 变量初始化后将调用您的函数(然后变量将保留函数的输出 ,这不是您想要的)。 Why not make another function that returns the output of your function? 为什么不制作另一个函数来返回函数的输出呢?

I'm bad at explaining things, so here's some code to stare at: 我不善于解释事物,因此下面是一些代码:

var my_var = function() { return my_func('called!'); };

The straightforward way to implement this would be wrapping a call to your function with an argumentless anonymous function: 实现此目的的直接方法是使用无参数匿名函数包装对函数的调用:

var my_var = new function() {
    my_func('called!');
}

ECMAScript 5th Edition introduces the Function.bind method that implements partial application (or more specifically currying ), that would let you write this the following way: ECMAScript 5th Edition引入了Function.bind方法,该方法实现了部分应用程序(或更具体地说,是currying ),您可以通过以下方式编写该方法:

var my_var = my_func.bind(undefined, 'called!');

(The undefined is there because the first parameter of bind() binds a value to the this "parameter".) (存在undefined位置,因为bind()的第一个参数bind()值绑定this “参数”。)

Function.bind() is relatively recent and not widely implemented. Function.bind()是相对较新的,尚未广泛实现。 The Mozilla documentation includes a simple shim you could use to get most of the functionality. Mozilla文档包含一个简单的填充程序,您可以使用它来获取大部分功能。 John Resig also has a blog post with a different implementation of partial application. John Resig也有一篇博客文章,其中介绍了部分应用程序的不同实现。 It might also be available in one of the many many JS libraries. 它也可能在许多JS库之一中提供。

You might be looking for something like Function.prototype.bind , which allows you to bind a function with arguments to a particular context. 您可能正在寻找类似于Function.prototype.bind东西,它允许您将带有参数的函数绑定到特定上下文。 Basically, it allows you to do this: 基本上,它允许您执行以下操作:

function myFunc(firstArg, secondArg) {
    alert(firstArg + secondArg);
};

var myVar = myFunc.bind(null, 'called!');

myVar(' - from myVar!');
// result is alert with -> "called! - from myVar"

It's not in older browsers, but the link above has a compatibility implementation to make it work for this particular scenario. 它不在较旧的浏览器中,但是上面的链接具有兼容性实现,使其可以在特定情况下使用。

Make a new function! 新增功能!

function my_func(first_arg) {
    alert(first_arg);
};
var my_var = function() {
    my_func('called!');
}

Just need the function to return a function: 只需要函数返回一个函数:

function my_func(first_arg) {
  return function(){alert(first_arg);}
};
var my_var = my_func('called!');

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

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