JavaScript 使用 类 创建角色的功能

JavaScript functionality to create roles with classes

我有一项任务是在 JS 中实现创建角色的功能,每个角色都是按其类型(来宾、用户、管理员)定义的。

  1. Type Guest 应该有:一个获取任务数组的方法(constructor); 从任务数组中获取任务 ID(数组索引)和 returns 任务的方法(getTaskcreateTask 不应定义 changeType
  2. Type User 也应该像 Guest 一样有 constructor,还有一个方法 getTask,和 createTask - 一种将新任务添加到任务数组的方法 changeType 不应被定义。
  3. 类型 Admin 应该有构造函数——一个获取来宾和用户数组的方法;

getArray-returns来宾和用户

数组的方法

changeType-获取数字(数组索引)的方法修改提供索引处的数组项(将对象类型从访客更改为用户,否则和因此,任务应该从一种类型迁移到另一种类型)和 returns 修改后的项目数组

所以,我创建了所有 classes 和方法,但是在最后,当我用 npm 测试它时,我得到的消息是:

但是当我在浏览器的控制台中引入它们时,一切正常,但我观察到当我写 guest.createTask 它显示它未定义并且不会抛出错误,但我认为我应该得到错误.

这是我的代码:

class Task {
    constructor(name) {
        this.name = name;
    }
}
class Guest {
    constructor(tasks) {
        this.tasks = tasks;
    }
    getTask(index) {
        return this.tasks[index];
    }
}

class User {
    constructor(tasks) {
        this.tasks = tasks;
    }
    getTask(index) {
        return this.tasks[index];
    }
    createTask(tasks) {
        return this.tasks.push(tasks);
    }
}

class Admin {
    constructor(userGuestArray) {
        this.userGuestArray = userGuestArray;
    }
    getArray() {
        return this.userGuestArray;
    }

    changeType(index, replace, arr) {
        replace = this.userGuestArray;
        arr = this.userGuestArray[index];
        if (replace[index] = Guest) {
            replace[index] = new User(arr.tasks);

        }if (replace[index] = User){
            replace[index] = new Guest(arr.tasks);

        }

        return replace;
    }
}

这里是测试访客的代码class:

const guest = new Guest(
    [
      new Task('task name 1'),
      new Task('task name 2'),
      new Task('task name 3'),
    ]
  );
expect(guest.getTask(0).name).to.equal('task name 1');
expect(guest.getTask(2).name).to.equal('task name 3');
const guest = new Guest([]);
expect(guest.createTask).to.throw('method \'createTask\' is not defined');
expect(guest.changeType).to.throw('method \'changeType\' is not defined');

下面是管理员更改类型角色:

admin = new Admin([
              new Guest([]),
              new Guest([new Task('task name 1')]),
              new User([]),
              new User([new Task('task name 2')]),
        ]);
 it('should change type of role from Guest to User', () => {
        expect(admin.getArray()[0] instanceof Guest).to.equal(true);
        admin.changeType(0);
        expect(admin.getArray()[0] instanceof User).to.equal(true);
    });

    it('should change type of role from User to Guest', () => {
        expect(admin.getArray()[3] instanceof User).to.equal(true);
        admin.changeType(3);
        expect(admin.getArray()[3] instanceof Guest).to.equal(true);
        
    });

    it('should move tasks from Guest to User, when change role', () => {
        expect(admin.getArray()[1].getTask(0)).to.deep.equal({ name: 'task name 1'});
        admin.changeType(1);
        expect(admin.getArray()[1].getTask(0)).to.deep.equal({ name: 'task name 1'});
    });

请帮助修复错误,我已经烧毁了我的大脑。我试图将方法设为私有,但我认为我不正确并且 work.I 不知道该怎么做。 PS:我是 JS 的新手,正在尝试学习这个任务。

这是理解作业的完整测试脚本:

const {Task, Guest, User, Admin} = require('../src');
const chai = require('chai');
const { expect } = chai;

describe('Guest', () => {
  it('should have method getTask', () => {
    const guest = new Guest(
        [
          new Task('task name 1'),
          new Task('task name 2'),
          new Task('task name 3'),
        ]
      );
      console.log(1,guest);
      expect(guest.getTask(0).name).to.equal('task name 1');
      console.log(2,guest.getTask(0).name);
      expect(guest.getTask(2).name).to.equal('task name 3');
      console.log(3,guest.getTask(2).name);
  });

  it('should not implement method createTask', () => {
    const guest = new Guest([]);
    console.log(4,guest);
    expect(guest.createTask).to.throw('method \'createTask\' is not defined');
    console.log(5,guest.createTask);
  });

  it('should not implement method changeType', () => {
    const guest = new Guest([]);
    console.log(6,guest);
    expect(guest.changeType).to.throw('method \'changeType\' is not defined');
    console.log(7,guest.changeType);
  });
});

describe('User', () => {
    let user;
    beforeEach(() => {
        user = new User(
            [
              new Task('task name 1'),
              new Task('task name 2'),
              new Task('task name 3'),
            ]
          );
    })
    console.log(8,user);
    it('should have method getTask', () => {
        expect(9,user.getTask(0).name).to.equal('task name 1');
        console.log(user.getTask(0).name)
        expect(10,user.getTask(2).name).to.equal('task name 3');
    });

    it('should have method createTask', () => {
        expect(user.getTask(0).name).to.equal('task name 1');
        console.log(11,user.getTask(0).name);
        expect(user.getTask(2).name).to.equal('task name 3');
        console.log(12,user.getTask(2).name);
        user.createTask(new Task('task name 4'))
        console.log(13,user.createTask(new Task('task name 4')));
        expect(user.getTask(3).name).to.equal('task name 4');
        console.log(14,user.getTask(3).name);
    });

    it('should not implement method changeType', () => {
        const user = new Guest([]);
        console.log(15,user);
        expect(user.changeType).to.throw('method \'changeType\' is not defined');
        console.log(16,user.changeType);
    });
});

describe('Admin', () => {
    let admin;
    beforeEach(() => {
        admin = new Admin([
              new Guest([]),
              new Guest([new Task('task name 1')]),
              new User([]),
              new User([new Task('task name 2')]),
        ]);
    })
    console.log(17,admin);
    it('should have method getArray', () => {
        expect(admin.getArray().length).to.equal(4);
    });
console.log(18,admin.getArray().length);
    it('should return instance of Guest as a first element by the method getArray', () => {
        expect(admin.getArray()[0]).to.deep.equal({ tasks: []});
    });
console.log(19,admin.getArray()[0]);
    it('should return instance of User as a third element by the method getArray', () => {
        expect(admin.getArray()[3]).to.deep.equal({ tasks: [{ name: 'task name 2'}]});
    });
console.log(20,(admin.getArray()[3]));
    it('should return array of roles by the method getArray', () => {
        expect(admin.getArray()).to.deep.equal([{ tasks: []}, { tasks: [{ name: 'task name 1'}]}, { tasks: []}, { tasks: [{ name: 'task name 2'}]}]);
    });
console.log(21,admin.getArray())
    it('should change type of role from Guest to User', () => {
        expect(admin.getArray()[0] instanceof Guest).to.equal(true);
        console.log(22,admin.getArray()[0] instanceof Guest);
        admin.changeType(0);
        console.log(23, admin.changeType(0));
        expect(admin.getArray()[0] instanceof User).to.equal(true);
        console.log(24,admin.getArray()[0] instanceof User);
    });

    it('should change type of role from User to Guest', () => {
        expect(admin.getArray()[3] instanceof User).to.equal(true);
        console.log(25,admin.getArray()[3] instanceof User);
        admin.changeType(3);
        console.log(26,admin.changeType(3));
        expect(admin.getArray()[3] instanceof Guest).to.equal(true);
        console.log(27,admin.getArray()[3] instanceof Guest)
    });

    it('should move tasks from Guest to User, when change role', () => {
        expect(admin.getArray()[1].getTask(0)).to.deep.equal({ name: 'task name 1'});
        console.log(28,admin.getArray()[1].getTask(0));
        admin.changeType(1);
        console.log(29,admin.changeType(1));
        expect(admin.getArray()[1].getTask(0)).to.deep.equal({ name: 'task name 1'});
        console.log(30,admin.getArray()[1].getTask(0));
    });

    
});

不要关注控制台日志,我尝试调试我的代码以查看错误在哪里。 错误日志:

        <?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Mocha Tests" time="0.0090" tests="13" failures="5">
  <testsuite name="Root Suite" timestamp="2021-11-06T12:36:46" tests="0" time="0.0000" failures="0">
  </testsuite>
  <testsuite name="Guest" timestamp="2021-11-06T12:36:46" tests="3" file="C:\Users\artio\Desktop Classes.2.classes\test\index.js" time="0.0040" failures="2">
    <testcase name="Guest should have method getTask" time="0.0010" classname="should have method getTask">
    </testcase>
    <testcase name="Guest should not implement method createTask" time="0.0010" classname="should not implement method createTask">
      <failure message="expected undefined to be a function" type="AssertionError"><![CDATA[AssertionError: expected undefined to be a function
    at Context.<anonymous> (test\index.js:21:38)
    at processImmediate (internal/timers.js:439:21)]]></failure>
    </testcase>
    <testcase name="Guest should not implement method changeType" time="0.0000" classname="should not implement method changeType">
      <failure message="expected undefined to be a function" type="AssertionError"><![CDATA[AssertionError: expected undefined to be a function
    at Context.<anonymous> (test\index.js:26:38)
    at processImmediate (internal/timers.js:439:21)]]></failure>
    </testcase>
  </testsuite>
  <testsuite name="User" timestamp="2021-11-06T12:36:46" tests="3" file="C:\Users\artio\Desktop Classes.2.classes\test\index.js" time="0.0010" failures="2">
    <testcase name="User should have method getTask" time="0.0000" classname="should have method getTask">
      <failure message="task name 1: expected 9 to equal &apos;task name 1&apos;" type="AssertionError"><![CDATA[AssertionError: task name 1: expected 9 to equal 'task name 1'
    at Context.<anonymous> (test\index.js:42:43)
    at processImmediate (internal/timers.js:439:21)]]></failure>
    </testcase>
    <testcase name="User should have method createTask" time="0.0000" classname="should have method createTask">
    </testcase>
    <testcase name="User should not implement method changeType" time="0.0000" classname="should not implement method changeType">
      <failure message="expected undefined to be a function" type="AssertionError"><![CDATA[AssertionError: expected undefined to be a function
    at Context.<anonymous> (test\index.js:55:41)
    at processImmediate (internal/timers.js:439:21)]]></failure>
    </testcase>
  </testsuite>
  <testsuite name="Admin" timestamp="2021-11-06T12:36:46" tests="7" file="C:\Users\artio\Desktop Classes.2.classes\test\index.js" time="0.0020" failures="1">
    <testcase name="Admin should have method getArray" time="0.0000" classname="should have method getArray">
    </testcase>
    <testcase name="Admin should return instance of Guest as a first element by the method getArray" time="0.0000" classname="should return instance of Guest as a first element by the method getArray">
    </testcase>
    <testcase name="Admin should return instance of User as a third element by the method getArray" time="0.0000" classname="should return instance of User as a third element by the method getArray">
    </testcase>
    <testcase name="Admin should return array of roles by the method getArray" time="0.0010" classname="should return array of roles by the method getArray">
    </testcase>
    <testcase name="Admin should change type of role from Guest to User" time="0.0000" classname="should change type of role from Guest to User">
      <failure message="expected false to equal true" type="AssertionError"><![CDATA[AssertionError: expected false to equal true
    at Context.<anonymous> (test\index.js:84:56)
    at processImmediate (internal/timers.js:439:21)]]></failure>
    </testcase>
    <testcase name="Admin should change type of role from User to Guest" time="0.0000" classname="should change type of role from User to Guest">
    </testcase>
    <testcase name="Admin should move tasks from Guest to User, when change role" time="0.0000" classname="should move tasks from Guest to User, when change role">
    </testcase>
  </testsuite>
</testsuites>

这是我完成这项任务的方法,一点也不难,但令人困惑:

class Task {
    constructor(name) {
        this.name = name;
    }
}
class Guest {
    constructor(tasks) {
        this.tasks = tasks;
    }
    getTask(index) {
        return this.tasks[index];
    }
    createTask() {
        throw new Error("method 'createTask' is not defined");
    }
    changeType() {
        throw new Error("method 'changeType' is not defined");
    }
}

class User {
    constructor(tasks) {
        this.tasks = tasks;
    }
    getTask(index) {
        return this.tasks[index];
    }
    createTask(tasks) {
        return this.tasks.push(tasks);
    }
    changeType() {
        throw new Error("method 'changeType' is not defined");
    }
}

class Admin {
    constructor(userGuestArray) {
        this.userGuestArray = userGuestArray;
    }
    getArray() {
        return this.userGuestArray;
    }

    changeType(index) {
       let mainArray = this.userGuestArray;
       let arr = this.userGuestArray[index];
        if (mainArray[index] instanceof Guest) {
            mainArray[index] = new User(arr.tasks);

        } else if (mainArray[index] instanceof User) {
            mainArray[index] = new Guest(arr.tasks);
        }
        return mainArray;
    }
}
module.exports.Task = Task;
module.exports.Guest = Guest;
module.exports.User = User;
module.exports.Admin = Admin;

对于错误部分,我应该只抛出一个,这就是为什么我在测试时遇到错误并显示我应该抛出错误的消息