What does this mean?
function fkey(a) {
a || (a = {});
if (!a.fkey) a.fkey = $("input[name='fkey']").attr("value");
return开发者_C百科 a
}
I guess a
is actually a function, but how to understand (!a.fkey)
?
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.
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')
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
).
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
精彩评论