Use a normal if
statement instead of the conditional operator:
if(!$.isArray(x)) {
x = [];
$('p').live('click', function(){
$('p').html('yay, it worked! Thankyou!')
});
}
Note: x
must be at least declared (var x;
) for this to work.
Reference: if...else
, jQuery.isArray
Update: Reading the comments on the other (now deleted answer), did you intend to do something like this?
function doStuff() {
$('p').html('yay, it worked! Thankyou!');
}
$('p').live('click', doStuff);
if(!$.isArray(x)) {
x = [];
doStuff();
}
It's not really clear given the information you provided. You just said run this function and posted a snippet which lets assume that the whole snippet is the code you want to run when the array is created. Please be more precise.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…