views:

66

answers:

4
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) ?

+2  A: 

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.

Nick Craver
@Mark - please don't correct the answer - I explicitly mean "falsy", **not** "false".
Nick Craver
@Nick, That is so weird that we both used the same terminology for it. ;-)
Jacob Relkin
@Jacob - That's the common term afaik: http://www.google.com/search?q=javascript+falsy
Nick Craver
@Nick - Sorry, never heard the term before so assumed it was a typo. You learn something every day ;)
Mark B
+1  A: 

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')

Jacob Relkin
A: 

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).

Victor Nicollet
A: 

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
galambalazs