Question

could someone explain me the logic behind these lines?

It is an onkeyup event. I don't know the value of r.

return $("#commentText").val() ?
(0==r&&(r=!0,setTimeout( function(){r=!1},1e4),socket.emit('status', connectiondetails)),void 0)
: !1

What does stand alone r=!0 do? What does function(){r!=1} do?

What is !1 as a return value?

is !1 == false?

Was it helpful?

Solution

This looks to be obfuscated or minified code.

return $("#commentText").val() ?
(0==r&&(r=!0,setTimeout( function(){r=!1},1e4),socket.emit('status', connectiondetails)),void 0)
: !1

becomes (as best I can figure it):

if ($("#commentText").val()) {
    if(0==r) {
        r = true;
        setTimeout( 
            function(){ r = false},1000
        );
        socket.emit('status', connectiondetails)
    }
}
else {
    return false;
}

Some mappings that are used:

true        -> !0     (saves 2 characters)
false       -> !1     (saves 3 characters)
if(x) { y } -> x && y (saves 5 characters)
1000        -> 1e4    (saves 1 character)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top