在我的数组中推送未定义的项目?

时间:2017-10-19 07:43:40

标签: javascript object

我正在创建一个对象并推送到一个数组this.reminders,但是未定义也会被推送为什么会这样?此代码将为我提供工作[ { description: 'Get to work in time', set_title: 'work related' }, undefined ]

   var ReminderSet = function(set_name) {
        this.set_name = set_name; 
        this.reminders = [];
    };

    ReminderSet.prototype.add = function(reminder, title) {
        this.reminders.push(reminder, title);
        console.log(this.set_name, this.reminders)

    };

    ReminderSet.prototype.list = function() {
        console.log(this.reminders); 
    };

    var Reminder = function(description,set_title) {
        this.description = description;
        this.set_title = set_title;
        //Describes the reminder
    };

var work = new ReminderSet("work")
          .add(new Reminder("Get to work in time", "work related"));

1 个答案:

答案 0 :(得分:3)

您的add()方法有两个参数:

ReminderSet.prototype.add = function(reminder, title) { /*...*/ }

但是你只传递了一个(即Reminder个对象):

var work = new ReminderSet("work")
      .add(new Reminder("Get to work in time", "work related"));

这会导致第二个参数未定义。