trinistorm Posted March 22, 2011 Share Posted March 22, 2011 I started back playing with JavaScript. I wanted to create an Ajax messaging library. The code below requests 3 parameters, the one called 'callback' expects a function. function runAjax(param, callback, formmode){ //param[0]=>uri param[1]=> Data, callback=>built in callback function, formode=>"GET","POST" switch console.log("Ajaxbot is in "+ajaxbot.readyState+" with http "+ajaxbot.status); if(ajaxbot != false){ if(ajaxbot.readyState != 0){ console.log("Still in state:"+ ajaxbot.readyState+"."); }else{ try{ if(formmode != true ){ ajaxbot.onreadystatechange = function(){ if(ajaxbot.readyState == 4 && ajaxbot.status ==200){ callback; } }; ajaxbot.open("GET", param[0]+param[1], true ); ajaxbot.send(); }else{ ajaxbot.onreadystatechange = function(){ if(ajaxbot.readyState == 4 && ajaxbot.status ==200){ callback; } }; ajaxbot.open("POST", param[0], true ); ajaxbot.setRequestHeader("Content-type","application/x-www-form-urlencoded"); ajaxbot.send(param[1]); } }catch(Ex){ console.log("Unable to access requested resource: "+param[0]," "+param[1]+"!"); console.log(Ex.toString()); } } }else{ console.log("Failed to get Ajax Object!"); }} After some time I realized that by passing runAjax() a callback function causes the callback to be executed as soon as runAjax() is called.So I'm thinking this is an improper use as of functions as parameters, how can I correct this so that my callback is only called during my selection statements. Link to comment Share on other sites More sharing options...
trinistorm Posted March 22, 2011 Author Share Posted March 22, 2011 Ok scratch, that I found a work around by creating an object and I just call the methods I need. However java script is strange as an OOP language, is there a way to make functions private? function AjaxObj(){ //Properties this.plaintext = null; this.xml = null; this.destid = null; //Methods this.dump = dump; this.parse = parseResponse; this.create = getAjaxbot;}function getAjaxbot(){ //Returns false if Ajax is not possible var ajaxbot = false; try{ ajaxbot = new XMLHttpRequest(); }catch(Ex){ try{ ajaxbot = new ActiveXObject("Msxml2.XMLHTTP"); }catch(Ex){ try{ ajaxbot = new ActiveXObject("Microsoft.XMLHTTP"); }catch(Ex){ } ajaxbot = false; } } return ajaxbot;}function parseResponse(callback){ //This is a method of AjaxObj console.log("Retriving Message..\n In state "+ajaxbot.readyState+" with http code "+ajaxbot.status); switch(callback){ case 0: //Do nothing break; case 1: //Dump the result to a div. AjaxObj.prototype.plaintext = ajaxbot.responseText; dump(AjaxObj.prototype.plaintext); break; }}function dump(Data){ //Dumps the server response to console and the {destid}. document.getElementById(AjaxObj.prototype.destid).innerHTML = Data; console.log("The Server said:\n"); console.log(AjaxObj.prototype.plaintext);} Link to comment Share on other sites More sharing options...
ShadowMage Posted March 22, 2011 Share Posted March 22, 2011 ...is there a way to make functions private?Just declare it inside the scope of your object:function myObject() { var privateVar = 'private'; function privateFunc() { //Do some stuff }} Link to comment Share on other sites More sharing options...
justsomeguy Posted March 22, 2011 Share Posted March 22, 2011 In your original code, you're not executing the callback correctly. Instead of just this: callback; you need this: callback(); You can also send that function parameters if you want, like the response object. To pass the function you just send the function name: function run(func){ func();}function test(){ alert('test');}run(test); // pass test function to run function You can also pass an anonymous function: run(function() { alert('test'); }); Link to comment Share on other sites More sharing options...
trinistorm Posted March 22, 2011 Author Share Posted March 22, 2011 callback();Thanks, I didn't realize I was doing that.function myObject() { var privateVar = 'private'; function privateFunc() { //Do some stuff }}Thanks, what I was looking for wasn't "private access" it would have been closer to "protected access". I got the functionality by using the "this" keyword in front of each declaration. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.