Brandon Brandon - 26 days ago 6
Javascript Question

Why isn't this anonymous function returning?

I know this is complete purposeless code I'm just experimenting with anonymous functions with code I have already written and had at hand. I can't figure out though why it array isn't returning?

(function() {
function Employee(name, age, pay) {
this.name = name;
this.age = age;
this.pay = pay || 800;
}

function Manager(name, age, pay) {
Employee.call(this, name, age, pay);
this.reports = [];
}
Manager.prototype = Object.create(Employee.prototype);
Manager.prototype.addReport = function(report) {
this.reports.push(report);
}

function Cashier(name, age, pay) {
Employee.call(this, name, age, pay);
}
Cashier.prototype = Object.create(Employee.prototype);
var ary = [Cashier, Manager];
return ary;
}());

Answer

...why it array isn't returning?

It is. You're just not doing anything with that return value; see *** comment on first line:

var result = (function() { // ****
    function Employee(name, age, pay) {
        this.name = name;
        this.age = age;
        this.pay = pay || 800;
    }

    function Manager(name, age, pay) {
        Employee.call(this, name, age, pay);
        this.reports = [];
    }
    Manager.prototype = Object.create(Employee.prototype);
    Manager.prototype.addReport = function(report) {
        this.reports.push(report);
    }

    function Cashier(name, age, pay) {
        Employee.call(this, name, age, pay);
    }
    Cashier.prototype = Object.create(Employee.prototype);
    var ary = [Cashier, Manager];
    return ary;
}());
console.log(result);

Comments