[英]Assigning functions to variables in javascript?
I used to have something like the following ... 我曾经有类似以下的东西......
var setFoo = function (value1) {
window.localStorage.setItem('foo1', value1);
}
var setFoo2 = function (value2) {
window.localStorage.setItem('foo2', value2);
}
.. however, I found that I was constantly doing ... ..然而,我发现我一直在做......
window.localStorage.setItem('something', value);
... so I decided to turn it into a function ... ......所以我决定把它变成一个功能......
function genericSetFoo(name, value) {
window.localStorage.setItem(name, value);
}
... and have the following instead ... ......而且还有以下内容......
var setFoo = genericSetFoo('foo1', value1);
var setFoo2 = genericSetFoo('foo2', value2);
... however, now when I try to call setFoo1(value1)
or setFoo2(value2)
, the application complains of value1
and value2
being undefined
. ...但是,现在当我尝试调用
setFoo1(value1)
或setFoo2(value2)
,应用程序会抱怨value1
和value2
undefined
。 What am I doing wrong? 我究竟做错了什么? I mean I could still do ...
我的意思是我还能做......
var setFoo = function(value1) { genericSetFoo('foo1', value1) };
var setFoo2 = function(value2) { genericSetFoo('foo2', value2) };
... and it works too... but it defeats the whole point if I have to redefine the function in any case. ......它也有效......但是如果我必须在任何情况下重新定义函数,它都会失败。 So how do I tackle this?
那么我该如何解决这个问题呢?
You can use .bind()
in this situation. 在这种情况下,您可以使用
.bind()
。 This technique is known as "partial function evaluation": 这种技术被称为“部分功能评估”:
var setFoo = genericSetFoo.bind(null, 'foo1');
var setFoo2 = genericSetFoo.bind(null, 'foo2');
setFoo(value1);
If you're using lodash or underscore, you can use _.partial()
which is different from .bind
in that it doesn't require that first argument and doesn't rebind the this
value: 如果您使用的是lodash或下划线,则可以使用与
.bind
不同的_.partial()
,因为它不需要第一个参数,也不会重新绑定this
值:
var setFoo = _.partial(genericSetFoo, 'foo1');
var setFoo2 = _.partial(genericSetFoo, 'foo2');
What am I doing wrong?
我究竟做错了什么?
You're calling window.localStorage.setItem()
instead of returning a function that calls window.localStorage.setItem()
. 你打电话
window.localStorage.setItem()
而不是返回调用一个函数window.localStorage.setItem()
Remember, when you call a function and assign it to a variable you are assigning the return value of that function , not the function itself. 请记住,当您调用函数并将其分配给变量时,您将分配该函数的返回值 ,而不是函数本身。
For example, when you do: 例如,当你这样做时:
function a () { return 2 };
var two = a();
.. do you expect the variable two
to be 2
or a function? ..你期望变量
two
是2
还是函数? Another example, if you do: 另一个例子,如果你这样做:
var x = document.getElementById('foo');
.. do you expect the variable x
to contain a DOM element or a function? ..你期望变量
x
包含DOM元素或函数吗?
So, you need to return a function instead of calling a function: 因此,您需要返回一个函数而不是调用函数:
function genericSetFoo(name) {
return function (value) {
window.localStorage.setItem(name, value);
}
}
So now you can do: 所以现在你可以这样做:
var setFoo1 = genericSetFoo('foo1');
What I am doing wrong?
我做错了什么?
When you do: 当你这样做时:
var setFoo = genericSetFoo('foo1', value1);
you are invoking genericSetFoo()
and assigning the value returned from it to setFoo
. 您正在调用
genericSetFoo()
并将从其返回的值分配给setFoo
。 However since value1
is undefined
at the time of initializing setFoo
(and thereby invoking genericSetFoo()
) you get an undefined
error when window.localStorage.setItem(name, value)
tries to use the argument passed to its value
parameter. 但是,由于
value1
在初始化setFoo
时undefined
(从而调用genericSetFoo()
),因此当window.localStorage.setItem(name, value)
尝试使用传递给其value
参数的参数时,会收到undefined
错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.