Javascript variable scope - local variable being overwritten -
i have folllowing code snippet:
function testfunction(selected) { console.log("selected id is: " + selected); var senddata={ id: selected[1], menu: true }; console.log(senddata); } when first run function testfunction(1), following output console:
selected id is: 1
the senddata object, , inside it, id set 1(correctly), menu set true(correctly), , have additional properties being added somewhere, don't know where
the second time run testfunction(2), following output console:
selected id is: 2
the senddata object, , inside it, id set 1(incorrectly), menu set true(correctly), , additionally added properties.
at first, tought senddata variable defined somewhere globally(as use variable name multiple times throughout script), searched through whole script, , no function accesses senddata global variable(i mean, wherever use senddata declare var senddata). if case, understanding, if declare variable in function "var" keyword, makes local variable, no global variable should interfere it. wrong in this? if no, problem causing error?
i still learning variable scoping in javascript, , have question. if name function paramater example "testvariable", , have global variable, named "testvariable", whenever call function , pass arguments it, global variable instantly gets changed?
edit:
here actual code snippet bigger function:
function mainsearchselect(e) { jquery("#navbar_mobile_menu_button").removeclass('fa-bars').addclass('fa-spin').addclass('fa-spinner'); jquery("#navbar_mobile_menu").removeclass("expanded"); var navendfunction=function() { selectselect2.find(".select2-icon").find("i").removeclass('fa-spinner').removeclass('fa-spin').addclass('fa-search'); jquery("#navbar_mobile_menu_button").removeclass('fa-spinner').removeclass('fa-spin').addclass('fa-bars'); } var select=jquery(e.currenttarget); var selectselect2=select.siblings(".select2"); var selected=e.params.args.data.id.split("-"); selectselect2.find(".select2-icon").find("i").removeclass('fa-search').addclass('fa-spinner').addclass('fa-spin'); var navdat={ pagedata: { pageid: selected[1] } }; console.log("selected id is: " + selected[1]); console.log(senddata); var senddata={}; console.log(senddata); senddata={ id: selected[1], menu: true }; console.log("data being sent:"); console.log(senddata); if (selected[0]=='cont') { navdat['pagedata']['page']='contacts'; navdat['pagedata']['module']='conversations'; convnavigate.navigate('loadpage', navdat, 'contacts', senddata, null, navendfunction); } else if (selected[0]=='group') { navdat['pagedata']['page']='groups'; navdat['pagedata']['module']='timeline'; convnavigate.navigate('loadpage', navdat, 'groups', senddata, null, navendfunction); } } mainsearchselect function being called select2 plugin, when option selected . e event being sent.
when arrive part in function console tells me following, strange in opinion:
"selected id is: 1"
undefined
object { }
"data being sent:"
object { id: "1", menu: true, javascript: true, accessdat: "{"pagedata":{"pageid":null,"page":null,"module":null,"moduleid":null},"parameters":["contacts",{"id":"1","menu":true},null,null],"urldata":{"title":null,"url":null},"mobiledata":{"title":null,"menu":null},"mobiletitle":"converser"}" }
what happening?
the access dat property being used in 2 places in script: first query sent select2, second added every ajaxcall make server.
i console logged e variable, , searched accessdat parameter, if being sent select2 aswell, isn't sent back, not e gets added.
the ajaxcall function gets called later on, here function does:
mainsearchselect(e)
gets fired when selection made select2, processes selection, , calls convnavigate.navigate() function, desired parameters in our case:
"loadpage" - string, tells function function should proceed when done
navdat - object, properties being sent historyjs change url , title of page
'contacts' - tells loadpage php file should access
senddata - object in question, data being sent server processing
null - start callback function should fired when loadpage loaded - null function should fire @ load
navendfunction - end callback function should fire when loadpage ready
here loadpage function:
function loadpage(page, senddata, startcallback, endcallback) { if (startcallback === undefined) startcallback=null; if (endcallback === undefined) endcallback=null; if ((senddata === undefined) || (senddata === null)) senddata={}; senddata['javascript']=true; console.log(senddata); ajaxcall(baseurl+"functions/"+page+".php", senddata, showpage, page, startcallback, endcallback); } note, senddata here identical previous logs, except javascript: true gets added correctly. ajaxcall function takes parameters file should loaded, data should sent, callback function should fired when ajaxcall ready , last 3 parameters parameters being sent callback function(page, startcallback, endcallback).
if help, write down function showpage well, don't think has problem:|
if comment out senddata['accessdat'] ajaxcall function, accessdat not being logged console in mainsearchselect function. part adds accessdat object, how possible, ajaxcall gets called later on, , so, console.log knows added it??? , also, resets id 1? (or first value being assigned to, because if select option id 3 select box first, gets stuck 3)
probably have typo in script selected[1] .you're acessing number array. senddata.id should set undefined.
Comments
Post a Comment