Setting Javascript Function Parameters inside the function parenthesis -
this simple forgot how it. i've passed variables function hence it's param's pre-set, need set param's when declaring function, don't remember setup.
i'm looking working version of this:
function(a,b=4){return a-b;}
where b
param' of function set when function declared.
if remember rightly it's setting default b
if function has no second argument:
function(a,b){b=b || 4; return a-b;}
edit
thanks seems it's impossible in js without ecmascript 6. answers getting bit off topic though... needed values set in paren's.
to keep on topic... initial problem sending parameters settimeout
function. ok have <div>
.gif
background, when clicked it's background changes, second animation runs 8 seconds , background changes again final .gif
. it's 3 stage animation, simple... thing 8sec gap, figured settimeout
work can't pass param's 'sto' function reference said <div>
.
if know of timer events can guest, far i've got. original code below... fails @ function(p = cha)
.
for(var = 0; < 5; i++){ var cha = document.createelement('div'); $(cha).css('background','url(img/stand.gif)'); cha.addeventlistener('click',function(){ $(cha).css('background','url(img/walk.gif)'); settimeout(function(p = cha){ $(p).css('background','url(img/walk.gif)'); },8000); }); }
function(a,b){b=b || 4; return a-b;}
this typical way default params in es5. advise changing check b's typs little more strictly, because 0 considered falsey value ||
operator
function(a,b){ b = typeof b === 'undefined' ? 4 : b; return a-b; }
Comments
Post a Comment