在 JS 中更改角色类型 类 的方法

method to change type of role classes in JS

所以我在 javascript 中有一个 类 的任务,我应该有一个带有构造函数和两个方法的 Admin 类型,一个是 getArray,其中 return 一组客人和用户,以及获取数字(数组索引)的方法 changeType 修改提供的索引处的数组项(将对象类型从访客更改为用户,否则,任务应从一种类型迁移到另一个)和 returns 修改后的项目数组。

为了更清楚地了解我的任务,这里是完整的描述:

这是实现的例子:

 const guest = new Guest(
      [
        new Task('task name 1'),
        new Task('task name 2'),
        new Task('task name 3'),
      ]
    );
    
    guest.getTask(0) // { name: 'task name 1' }
    guest.getTask(2) // { name: 'task name 3' }
    guest.createTask(new Task('task name 4')) // taskName is not defined, should not work
    
const user = new User(
  [
    new Task('task name 1'),
    new Task('task name 2'),
    new Task('task name 3'),
  ]
);

user.getTask(0) // { name: 'task name 1' }
user.getTask(2) // { name: 'task name 3' }
user.createTask(new Task('task name 4'))
user.getTask(3) // {name: 'task name 4'}

const admin = new Admin(
  [
    new Guest([]),
    new Guest([new Task('task name 1')]),
    new User([]),
    new User([new Task('task name 2')]),
  ]
);

admin.getArray(); // [Guest, Guest, User, User]
admin.changeType(1);
admin.getArray(); // [Guest, User, User, User]

我对剩下的任务没有问题,我的问题是 changeType method.It 以某种方式在浏览器中给了我预期的结果,但是当我用 npm 测试它说 Admin 应该将角色类型从 User 更改为 Guest 。 这是我的实现:

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

        } else {
            replace[index] = new Guest(arr.tasks);
        }
        return replace;
    }
}

这是测试:

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')]),
        ]);
    })
    it('should have method getArray', () => {
        expect(admin.getArray().length).to.equal(4);
    });
    it('should return instance of Guest as a first element by the method getArray', () => {
        expect(admin.getArray()[0]).to.deep.equal({ tasks: []});
    });
    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'}]});
    });
    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'}]}]);
    });
    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'});
    });
});

你们能帮我解决一下这个方法吗?这是提交任务我应该做的最后一件事,但我不知道该怎么做。提前致谢!

因此,在阅读了评论并搜索了文档之后,我重写了该函数并使其运行。

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