JavaScript:闭包中的自动获取器和设置器没有eval? [英] JavaScript: automatic getters and setters in closures without eval?
问题描述
注意:关于JavaScript中的getter和setter的问题很多(请参见javascript), JavaScript中的字母和字母(正确的语法?), javascript中的自动getter和setter(带有验证), 等等).这些问题大多数涉及对象 Objects
,一些涉及 Closures
,很少涉及自动生成getter和set的问题.这个问题将在后面讨论.
Note: there are a lot of questions regarding getters and setters in JavaScript (see Javascript Getters And Setters, Getters and Setters in a function (javascript), Getters and Setters in JavaScript (Correct syntax ?), automatic getter and setter(with validation) in javascript, etc). Most of these questions deal with Objects
, a few with Closures
and rather few of them with automatically producing getters and sets. This question deals with the later.
answer 到
The answer to automatic getter and setter(with validation) in javascript is perhaps the closest for what I am looking for, but not-ideal for my use case.
当前,在我关闭时,我有以下内容:
Currently, in my closure I have the following:
function myClosure() {
var closureVar1, closureVar2
function do () { ... }
function setupSettersAndGetters() {
var closureVariables = ['closureVar1', 'closureVar2']
for (var i = 0; i < closuredVariables.length; i++) {
var currentClosureVariable = closureVariable[i]
var toEvaluateString = " \
do." + currentClosureVariable + " = function(value){ \
if(!arguments.length) return " + currentClosureVariable + "; \
" + currentClosureVariable + " = value; \
return setup; \
}; \
"
eval(toEvaluateString)
}
}
setupSettersAndGetters()
return do;
}
这完全符合我的意愿,例如
This works exactly as I like, e.g.
var test = myClosure()
test.closureVar1(10).closureVar2(2)
test.closureVar1() + test.closureVar2() // return 12
使用 eval
只是一个微小的"问题,有些人测试.
There is just the one "tiny" problem of using eval
which some people detest.
所以我的问题是,有没有更好的方法来处理此问题,并允许我按名称而不是使用字符串来调用setter/getter?
So my question is, is there a better way to handle this and allow for me to call the setter / getter by name, rather than using a string?
例如我可以使用一个中间函数,例如:
e.g. I could use an intermediate function such as:
test.sget('closureVar1', 10) // set closureVar1 to 10
test.sget('closureVar2') // get closureVar2
但是即使这样做,我仍然需要使用 eval
?而且我更喜欢调用变量的名称...
but even doing that I think I still need to use eval
? and I would prefer calling the name of the variable...
有想法吗?
推荐答案
您可以采用传递给 myClosure
函数的参数...
You can take the arguments passed to your myClosure
function...
var args = Array.prototype.slice.call(arguments);
...并使用它们来设置您的setter和getter:
... and use them to set up your setters and getters:
args.forEach(function(arg) {
instance[arg] = function(d) {
if (!arguments.length) return arg;
arg = d;
return instance;
};
});
这样,您可以传递所需的变量数量,并避免使用 eval
.
That way, you can pass how many variables you want and also avoiding eval
.
这是一个演示:
function myClosure() {
var instance = {};
var args = Array.prototype.slice.call(arguments);
args.forEach(function(arg) {
instance[arg] = function(d) {
if (!arguments.length) return arg;
arg = d;
return instance;
};
})
return instance;
};
var test = myClosure("v1", "v2", "v3")
test.v1(16).v2(2).v3(8)
console.log(test.v1() + test.v2() + test.v3())
这篇关于JavaScript:闭包中的自动获取器和设置器没有eval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!