Bin Chen
Bin Chen

Reputation: 63299

What does this mean?

function fkey(a) {
    a || (a = {});
    if (!a.fkey) a.fkey = $("input[name='fkey']").attr("value");
    return a
}

I guess a is actually a function, but how to understand (!a.fkey) ?

Upvotes: 0

Views: 125

Answers (4)

Nick Craver
Nick Craver

Reputation: 630389

a is an object in this case, it's setting the .fkey property on it if it isn't set (or is falsy) already.

For SO chat, this allows the fkey input to either be provided or gotten from the page, it's a hidden input at the bottom of your page, populated with a value used to authenticate your request and such.

Currently it's always pulling from the DOM, so really this function just adds the property, it would leave it alone if it were provided though.

Upvotes: 2

gblazex
gblazex

Reputation: 50109

The function takes an object and adds an fkey property to it, which will be the value of
<input name="fkey"> field.

For example:

<input name="fkey" value="WATCH ME HERE">

var temp = {};    // make a new object
fkey(temp);       // expand it with fkey's value
alert(temp.fkey); // WATCH ME HERE

Upvotes: 0

Victor Nicollet
Victor Nicollet

Reputation: 24577

The function you posted adds a member named fkey to a if it did not already exist. The !a.fkey part essentially means "does not already exist" (this works because once assigned, the member does not evaluate to false).

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163238

a is not a function, it's an object.

The a.fkey is accessing a member of the a object. The ! in front means that if the member does not exist or the value is falsy, the expression evaluates to true and the fkey member is set to $("input[name='fkey']").attr('value');, which can also be accomplished with .val() instead of .attr('value')

Upvotes: 1

Related Questions