如何正确关联这个对象的两个构造函数?

时间:2017-10-17 01:35:04

标签: javascript oop inheritance

我试图用javascript创建我自己的提醒应用程序,所以基本上我可以创建一组提醒,如类别(工作,学校,编程提醒等)。并且每个都可以在工作类别中提醒他们,我可以在那里提供提醒列表等等。我还想在本地存储上保存集合及其提醒,并在页面上重新创建它们。当然,提醒列表应该在它所属的集合上。

在我做这个页面之前,我真的想先对这个进行建模,然后我决定继续使用oop方法作为练习,我想知道如何将它们联系起来,这是我到目前为止所得到的......

    var ReminderSet = function(set){
      this.set = set; //could be work, school , etc 

    }


    var Reminders = function(new_reminder){
      this.reminders = new_reminder; // a new reminder
    }

    ReminderSet.prototype.reminders = new Reminders();

    ReminderSet.prototype.printSet = function(){
      console.log(this.set, this.reminders); /* gives the ff output 
work { reminders: 'finish task on time' }
school { reminders: 'study on chem' }*/
    }

    var workReminders = new ReminderSet("work"); // create a new set of reminder
    var schoolReminders = new ReminderSet("school"); // create a new set of reminder

    workReminders.reminders = new Reminders("please the boss"); //add a new reminder to work
    workReminders.reminders = new Reminders("finish task on time");//add a new reminder to work
    schoolReminders.reminders = new Reminders("finish projects"); //add new reminder to school
    schoolReminders.reminders = new Reminders("study on chem"); //add new reminder to school

    workReminders.printSet(); 
    schoolReminders.printSet();

问题是它不会让我为该组创建多个提醒,我希望你能帮助我这个谢谢你!为了清楚起见我只学习了3个月的javascript,这是我第一次做oop虽然我以前做过一些以前的任务项目,但我认为真正熟悉oop非常重要。

3 个答案:

答案 0 :(得分:0)

尝试这个,在创建新实例时使用函数作为模板填充,而不是将它们作为自己的变量。

   function ReminderSet(set){
      this.set = set; //could be work, school , etc 

    }


    function Reminders(new_reminder){
      this.reminders = new_reminder; // a new reminder
    }

    ReminderSet.prototype.reminders = new Reminders();

    ReminderSet.prototype.printSet = function(){
      console.log(this.set, this.reminders); /* gives the ff output 
work { reminders: 'finish task on time' }
school { reminders: 'study on chem' }*/
    }

    var workReminders = new ReminderSet("work"); // create a new set of reminder
    var schoolReminders = new ReminderSet("school"); // create a new set of reminder

    workReminders.reminders = new Reminders("please the boss"); //add a new reminder to work
    workReminders.reminders = new Reminders("finish task on time");//add a new reminder to work
    schoolReminders.reminders = new Reminders("finish projects"); //add new reminder to school
    schoolReminders.reminders = new Reminders("study on chem"); //add new reminder to school

    workReminders.printSet(); 
    schoolReminders.printSet();

答案 1 :(得分:0)

您可以将数组设置为class的属性,并使用一个对象,其中属性名称是提醒名称,值可以是任何类型



class Reminders {
  constructor() {
    this.reminders = [];
  }
  setReminder(key, prop) {
    const reminder = {
      type: key,
      value: prop
    };
    this.reminders.push(reminder);
    return this;
  }
  getReminder(key) {
    return this.reminders.filter(({type}) => type === key)
  }
}

let reminders = new Reminders()

reminders.setReminder("work", ["a", "b"]);

let work = reminders.getReminder("work");

reminders.setReminder("school", "study")

let school = reminders.getReminder("school");

reminders.setReminder("do stuff", "stuff")

let stuff = reminders.getReminder("do stuff");

for (let [{type, value}] of Object.values([work, school, stuff])) {
  console.log(`reminder type:${type}, reminder value:${value}`)
}




答案 2 :(得分:0)

您只能添加一个提醒的原因是因为您没有构建提醒集合。每次设置新提醒时,都会覆盖之前的提醒。

我已经重新编写了您发布的代码,使用我认为更直观的属性名称,还要在每个public class OddsAndEvens { // counts all the odd numbers in the array private static int countOdds(int[] array) { int count = 0; for(int i = 0; i < array.length; i++) { if(array[i] % 2 == 1 && array[i] % 2 != 0) { count++; } } return count; } // returns an array with all the odd numbers public static int[] getAllOdds(int[] array) { int[] yaArray = new int[countOdds(array)]; int j = 0; for(int i = 0; i < array.length; i++) { if(array[i] % 2 == 1 && array[i] % 2 != 0) { yaArray[j] = array[i]; } j++; } return yaArray; } } /////////////////////////////////////////////////////////// runner code public class OddsAndEvensRunner { public static void main(String args[]) { System.out.println("Odds - " + Arrays.toString(OddsAndEvens.getAllOdds(new int[]{2,4,6,8,10,12,14}))); System.out.println("Evens - " + Arrays.toString(OddsAndEvens.getAllEvens(new int[]{2,4,6,8,10,12,14}))); System.out.println("\nOdds - " + Arrays.toString(OddsAndEvens.getAllOdds(new int[]{1,2,3,4,5,6,7,8,9}))); System.out.println("Evens - " + Arrays.toString(OddsAndEvens.getAllEvens(new int[]{1,2,3,4,5,6,7,8,9}))); System.out.println("\nOdds - " + Arrays.toString(OddsAndEvens.getAllOdds(new int[]{2,10,20,21,23,24,40,55,60,61}))); System.out.println("Evens - " + Arrays.toString(OddsAndEvens.getAllEvens(new int[]{2,10,20,21,23,24,40,55,60,61}))); } } 内构建一组提醒。

&#13;
&#13;
ReminderSet
&#13;
&#13;
&#13;