I'm completely new to JavaScript. Can anyone help me understand why this code isn't working, and what changes I need to make to fix it?
function getResults(keywords) {
foo.foo = function() {
var bar = foo.getSomeText; // Contains "blabla"
};
return bar;
}
// Global scope
alert(bar); // Does nothing
Edit (apologies for the lack of information):
The issue is that I am trying to retrieve text from an XHR request and I need to use a function to handle the onreadystatechange event. Here is the original code:
function getResults(keywords) {
// Make a request and get results
var xhr = new XMLHttpRequest();
xhr.open('GET', './autoc.php?s='+ encodeURIComponent(keywords));
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = xhr.responseText;
var test = response.split('|');
}
};
xhr.send(null);
return test;
}
var hum = getResults('test');
console.log(hum);