首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >输出不正确,不确定如何修复

输出不正确,不确定如何修复
EN

Stack Overflow用户
提问于 2019-10-07 15:25:07
回答 3查看 52关注 0票数 0

我必须有两个论证,名称和部门,并创建了一个默认名称未知和部门未知的name和dept实例变量,然后为它们创建get和set方法,但是每次运行这个方法时,它都会给出类名和set是未定义的。

最初,我没有把它放在一个类中,把它作为一个直接的const函数,它正在工作,但是当我不能在另一个文件中正确地引用它时,我被告知他们需要放在我尝试过的类中,而现在它没有给出我所需要的输出。

代码语言:javascript
复制
class Faculty {
    constructor(name, dept) {   
        this.name = "name unknown";
        this.dept = "department unknown";
    }
    getName() {
        return this.name;
    }
    getDept() {
        return this.dept;
    }

    setName(name) {
        this.name = name;

    }
    setDept(dept) {
        this.dept = dept;
    }
}

Faculty.toString = function () {
        return this.name.concat(" of ").concat(this.dept);
}
//Faculty.name = "Testname";
//Faculty.dept = "Electronic Technology";
console.log(Faculty.toString());

当运行时,它给了学院的未定义,即使当我试图定义名称,它仍然只是说学院,虽然我需要它的默认名称未知的部门名称未知,除非另有规定。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-10-07 15:29:16

下面是我要说的话(而且起作用了)

编辑:由于选择了这个答案,我将在这里添加其他答案所指出的好元素。

代码语言:javascript
复制
const DEFAULT_NAME = "name unknown";
const DEFAULT_DEPT = "department unknown";

class Faculty {
    constructor(name = DEFAULT_NAME, dept DEFAULT_DEPT) {   
        this.name = name;
        this.dept = dept;
    }
    getName() {
        return this.name;
    }
    getDept() {
        return this.dept;
    }

    setName(name) {
        this.name = name;

    }
    setDept(dept) {
        this.dept = dept;
    }

    toString() {
        return `${this.name} of ${this.dept}`;
    }
}

const f = new Faculty("Faculty", "Department");
console.log(f.toString());
票数 1
EN

Stack Overflow用户

发布于 2019-10-07 15:41:09

此外,还可以使用默认的参数,如下所示:

代码语言:javascript
复制
class Faculty {
  constructor(name = 'name unknown', dept = 'department unknown') {
    this.name = name;
    this.dept = dept;
  }
  getName() {
    return this.name;
  }
  getDept() {
    return this.dept;
  }

  setName(name) {
    this.name = name;
  }
  setDept(dept) {
    this.dept = dept;
  }

  toString() {
    return `${this.name} of ${this.dept}`;
  }
}

const f = new Faculty('Alex', 'Maths');
console.log(f.toString());
票数 1
EN

Stack Overflow用户

发布于 2019-10-07 15:47:25

首先,您必须创建一个Faculty的新实例,以调用其类方法之一。

第二,不需要在类之外声明toString方法;它可以和其他方法一样包括在内。

第三,我认为使用模板文字可以简化/澄清方法本身。

代码语言:javascript
复制
const DEFAULT_NAME = "name_unknown";
const DEFAULT_DEPARTMENT = "department_unknown";

class Faculty {
    constructor(name, dept) {   
        this.name = name || DEFAULT_NAME;
        this.dept = dept || DEFAULT_DEPARTMENT;
    }
    getName() {
        return this.name;
    }
    getDept() {
        return this.dept;
    }

    setName(name) {
        this.name = name;
    }
    
    setDept(dept) {
        this.dept = dept;
    }
    
    toString() {
        return `${this.name} of ${this.dept}`
    }
}

//With name and department
const faculty = new Faculty("John Smith", "Department XYZ");
console.log(faculty.toString());

//Without name and department
const faculty_default = new Faculty();
console.log(faculty_default.toString());

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58272621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档