是否可以使用三元运算符来声明函数名称?
var foo,
bar = 'bar';
(foo || bar) = function(){ // Invalid left-hand side in assignment [Bad assignment]
alert(true);
};
[foo || bar] = function(){ // Invalid left-hand side in assignment [Bad assignment]
alert(true);
};
(foo ? foo : bar) = function(){ // Invalid left-hand side in assignment [Bad assignment]
alert(true);
};
最佳答案
this[foo || bar] = function(){alert(true)}
事情是,如果 bar
等于“bar”,你将用一个函数覆盖自己......
关于javascript - 带三元运算符的函数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14495559/