2

I often rely on the fact that in Javascript conditional operators do not return true or false but one of the arguments.

For example:

function(e){
    this.someotherfunction.apply(this, e.config || []); 
}

So far I have not come across any issues and when I tried to find out what Douglas Crockford (read JSLint) has to say about it, I haven't found any issue.

Are there any dangers in using this kind of syntax?

4

4 回答 4

3

Are there any dangers in using this kind of syntax ?

No.

(As long as you and all other developers know what this code does...)

于 2012-07-03T21:06:27.900 回答
1

这在许多动态语言中都有完整的文档记录并以这种方式工作。使用这种结构是安全的,并且被广泛认可。

于 2012-07-03T21:09:31.173 回答
1

该方法是惯用的 JavaScript。

使用这种语法有什么危险吗?

你必须小心知道输入是什么。例如,如果某物需要一个数字,那么您必须说明0它是一个错误的值。

于 2012-07-03T21:09:37.627 回答
1

您可能面临的唯一问题是愤怒的开发人员说他们很难阅读并要求将其重写为。

foo : function (e) {
    var config =  e.config || [];
    this.someotherfunction.apply(this,config); 
}

是的,我遇到了那些愤怒的开发者。

于 2012-07-03T21:35:30.410 回答