为什么这个匿名函数不会返回?

时间:2016-11-06 08:17:22

标签: javascript

我知道这是完全没用的代码我只是尝试使用我已经编写过的代码的匿名函数。我无法弄清楚为什么数组没有返回?

(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;
}());

2 个答案:

答案 0 :(得分:1)

  

...为什么数组没有返回?

是的。你只是没有做任何有回报价值的事情;请参阅第一行***评论:

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);

答案 1 :(得分:1)

实际上,此代码返回两个构造函数对象。尝试在控制台上运行它: -

enter image description here