JavaScript: Not always what you expect

<Krishean> i found out something about arrays not too long ago
<Krishean> if you pass an array as an argument it only passes a reference to the array
<Krishean> as a function argument
<Krishean> hold on i'll get a example
<Krishean> function change(a){a[0]=2;}function main(){var a=[1];change(a);alert(a[0]);}main();
<Krishean> run that code and figure out wtf its doing
<app> not now ...i am busy being lazy
<hollow87> yo
<Krishean> heya hollow
<hollow87> sup
<Krishean> function change(a){a[0]=a[0].split('').reverse().join('');}
<Krishean> var arr=new Array();
<Krishean> arr[0]='doof';
<Krishean> change(arr);
<Krishean> alert(arr[0]);
<Krishean> thats a little more fun
<Krishean> not much
<Krishean> trying to teach an old app new tricks
<Krishean> 'doof' -> [???] -> 'food'
<Krishean> function obama(a){a[0]=a[0].split('').reverse().join('');} // its change, but not as you may expect
<Gunrunner_Gray> lol
<Krishean> its life jim, but not as we know it
<Krishean> gray have you learned any js?
<Gunrunner_Gray> Nope, not yet
<Krishean> huh
<Krishean> i'd think by being around me long enough it would catch like a bad case of the flu

0 comments: