0

I get desired output from below code. These are two JavaScript function expressions for printing data

(function(){
var getData=function()
{
   console.log("getdata");
},
setData=function()
{
  getData();
  console.log("setData");
};
setData();
})();

But when I try something like this in another page.I didn't get desired output.

This is my code.

var employeesList = {
    getemployees: function () {
    var data= getData("Employees/GetEmployees");
    },
    init: function () {
        this.getemployees();
    }
}.init();

  var getData = function (url) {
    var error = false;

    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'json',
        success: function (data) {
            return data;
        },
        error: function () {
            return error = true;
        }

    });
};

I got an error like this. getData is not a function please help.

6
  • 2
    move var getData = function (url) { above var employeesList = { Commented Apr 26, 2017 at 6:43
  • Make sure function is defined or accessible in that scope. Commented Apr 26, 2017 at 6:44
  • Or declare it like function functionName() {} instead of using a variable. That way it'll be usable before being declared. Commented Apr 26, 2017 at 6:45
  • Now it is working.i forget the concept JavaScript Hoisting. Thanks for remembering me. Commented Apr 26, 2017 at 6:45
  • function functionName() {} this is function declaration. This will initialize on initial function call. Commented Apr 26, 2017 at 6:49

2 Answers 2

1
  • You can't use a variable before have defined it, so you have to declare getData before employeesList
  • The variable data in getemployees couldn't be accessible, I added a return there, so you can use its value in init

var getData = function (url) {
    var error = false;

    /*$.ajax({
        type: 'GET',
        url: url,
        dataType: 'json',
        success: function (data) {
            return data;
        },
        error: function () {
            return error = true;
        }

    });*/
    return 'test_result';
};

var employeesList = {
    getemployees: function () {
        //Here you should return the result
        return getData("Employees/GetEmployees");
    },
    init: function () {
        var res = this.getemployees();
        console.log(res);
    }
}.init();

I hope it was clear, bye.

Sign up to request clarification or add additional context in comments.

Comments

0

There are two methods to define "function":

  1. var func_name = function(){...}
  2. function func_name() {...}

The difference is that when you use the second one, it can be called before being declared.

var employeesList = {
    getemployees: function () {
    var data= getData("Employees/GetEmployees");
    },
    init: function () {
        this.getemployees();
    }
}.init();

  //var getData = function (url) {
function getData (url) { // <====== change to this
    var error = false;

    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'json',
        success: function (data) {
            return data;
        },
        error: function () {
            return error = true;
        }

    });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.