当前位置: 动力学知识库 > 问答 > 编程问答 >

JavaScript confirm window

问题描述:

I am reviewing some code around a JavaScript confirm call. From my understanding, confirm returns true or false. The developer I am working with keeps doing the following:

function askTheUser(question, myCallback) {

var result = confirm(question);

myCallback(result ? 2 : 1);

}

The line that is throwing me for a loop is the result ? 2 : 1. Why would someone do that instead of just myCallback(result);

Is there something related to callbacks that I'm not aware of. I'm just interested in returning the true or false associated with whether a user confirmed the question or not. I keep looking at it. It just looks incorrect.

Thank you.

网友答案:

result ? 2 : 1 means that if result is true then 2 is returned otherwise 1

He/She must have a special reason to do so. Try looking the code where it's being used.

网友答案:

It's not incorrect, it's just converting true/false into 2/1. Presumably they have some reason for doing that.

网友答案:

Yes you're right. It should just be (result). The callback can then assign 1, 2 or whatever it wants - if its really necessary to do so! It is simply going to invoke the callback with the parameter, there's nothing special about it

分享给朋友:
您可能感兴趣的文章:
随机阅读: